C# 上载图像-外键冲突

C# 上载图像-外键冲突,c#,mysql,asp.net,sql,asp.net-mvc,C#,Mysql,Asp.net,Sql,Asp.net Mvc,我正在尝试使用jquery插件上传我的图片并将其插入我的数据库 我的问题是这两张桌子之间的关系 INSERT语句与外键约束冲突 “FK_图像_用户”。数据库中发生冲突 “Mydatabase”,表“dbo.User”,列“Id” 这是我的密码: 在控制器中: 获取: 要上载,我有以下代码: private void UploadWholeFile(HttpContext context, List<FilesStatus> statuses, Image img, User u)

我正在尝试使用jquery插件上传我的图片并将其插入我的数据库

我的问题是这两张桌子之间的关系

INSERT语句与外键约束冲突 “FK_图像_用户”。数据库中发生冲突 “Mydatabase”,表“dbo.User”,列“Id”

这是我的密码:

在控制器中:

获取:

要上载,我有以下代码:

 private void UploadWholeFile(HttpContext context, List<FilesStatus> statuses, Image img, User u)
        {
            using (SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
            {
                for (int i = 0; i < context.Request.Files.Count; i++)
                {
                    var file = context.Request.Files[i];

                    var fullpath = StorageRoot + Guid.NewGuid() + Path.GetFileName(file.FileName);

                    file.SaveAs(fullpath);

                    string fullName = Path.GetFileName(file.FileName);
                    statuses.Add(new FilesStatus(fullName, file.ContentLength, fullpath));

                    SqlCommand cmd;
                    System.Text.StringBuilder sql = new System.Text.StringBuilder();
                    sql.Append("insert into Image(MyFileName,Id_User)");
                    sql.Append("values (@MyFileName, @Id_User)");

                    cn.Open();
                    cmd = new SqlCommand(sql.ToString(), cn);
                    cmd.Parameters.Add("@MyFileName", SqlDbType.VarChar).Value = fullpath;
                    cmd.Parameters.Add("@Id_User", SqlDbType.Int).Value = u.Id;

                    cmd.ExecuteNonQuery();

                    cn.Close();
                }

            }
        }
private void UploadWholeFile(HttpContext上下文、列表状态、图像img、用户u)
{
使用(SqlConnection cn=新的SqlConnection(System.Configuration.ConfigurationManager.ConnectionString[“DefaultConnection”].ConnectionString))
{
for(int i=0;i
Id\u用户与dbo.User表存在外键关系,该表不包含您试图插入的Id。首先在dbo.User表中插入值,或者检查并更正您在Id\u User中插入的值。

请确保您在Image表中插入了Id\u User的正确值,该值也应该出现在User表中。我无法理解的是。我有一个get方法,它发送表的值。然后在上传时,他无法获得当前id的值
 private void UploadWholeFile(HttpContext context, List<FilesStatus> statuses, Image img, User u)
        {
            using (SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
            {
                for (int i = 0; i < context.Request.Files.Count; i++)
                {
                    var file = context.Request.Files[i];

                    var fullpath = StorageRoot + Guid.NewGuid() + Path.GetFileName(file.FileName);

                    file.SaveAs(fullpath);

                    string fullName = Path.GetFileName(file.FileName);
                    statuses.Add(new FilesStatus(fullName, file.ContentLength, fullpath));

                    SqlCommand cmd;
                    System.Text.StringBuilder sql = new System.Text.StringBuilder();
                    sql.Append("insert into Image(MyFileName,Id_User)");
                    sql.Append("values (@MyFileName, @Id_User)");

                    cn.Open();
                    cmd = new SqlCommand(sql.ToString(), cn);
                    cmd.Parameters.Add("@MyFileName", SqlDbType.VarChar).Value = fullpath;
                    cmd.Parameters.Add("@Id_User", SqlDbType.Int).Value = u.Id;

                    cmd.ExecuteNonQuery();

                    cn.Close();
                }

            }
        }