Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 4.0 实体框架插入具有相关对象的对象_C# 4.0_Entity Framework 4 - Fatal编程技术网

C# 4.0 实体框架插入具有相关对象的对象

C# 4.0 实体框架插入具有相关对象的对象,c#-4.0,entity-framework-4,C# 4.0,Entity Framework 4,我是实体框架的新手,需要在数据库中插入一个对象Comment,该对象具有相关的FK对象User public Class Comment { public int CommentID { get; set; } public string CommentContent { get; set; } public virtual User User { get; set; } public virtual DateTim

我是实体框架的新手,需要在数据库中插入一个对象
Comment
,该对象具有相关的FK对象
User

    public Class Comment
    {
        public int CommentID { get; set; }
        public string CommentContent { get; set; }
        public virtual User User { get; set; }
        public virtual DateTime CommentCreationTime { get; set; }
    }

public class User
{      

    public int UserID { get; set; }
    public string UserName { get; set; }
    public string UserPassword { get; set; }

    public string UserImageUrl{get; set;}
    public DateTime UserCreationDate { get; set; }

    public virtual List<Comment> Comments { get; set; }
}

  public void AddComment()
  {
        User user = new User() { UserID = 1 };            
        Comment comment = new Comment() { CommentContent = "This is a comment", CommentCreationTime = DateTime.Now, User = user };

        var ctx = new WallContext();
        comments = new CommentsRepository(ctx);

        comments.AddComment(comment);
        ctx.SaveChanges();
   }
公共类注释
{
public int CommentID{get;set;}
公共字符串内容{get;set;}
公共虚拟用户用户{get;set;}
公共虚拟日期时间CommentCreationTime{get;set;}
}
公共类用户
{      
public int UserID{get;set;}
公共字符串用户名{get;set;}
公共字符串UserPassword{get;set;}
公共字符串UserImageUrl{get;set;}
公共日期时间UserCreationDate{get;set;}
公共虚拟列表注释{get;set;}
}
public void AddComment()
{
User User=new User(){UserID=1};
Comment Comment=new Comment(){CommentContent=“这是一条评论”,CommentCreationTime=DateTime.Now,User=User};
var ctx=新的WallContext();
评论=新评论回复(ctx);
评论。添加评论(评论);
ctx.SaveChanges();
}
理想情况下,使用T-SQL,如果我知道我的
User
对象的主键,我可以插入我的
Comment
对象,并在insert语句中指定我的“User”的主键

我尝试过用实体框架做同样的事情,但它似乎不起作用。如果必须先从数据库中获取
User
对象,然后插入新的“注释”,那就太过分了


请问,我如何才能做到这一点?

您需要将用户对象附加到上下文,以便上下文知道它是一个现有实体

  public void AddComment()
  {
       var ctx = new WallContext();

        User user = new User() { UserID = 1 };  

        ctx.Users.Attach(user);

        Comment comment = new Comment() { CommentContent = "This is a comment", CommentCreationTime = DateTime.Now, User = user };

        comments = new CommentsRepository(ctx);

        comments.AddComment(comment);
        ctx.SaveChanges();
   }

将用户附加到上下文不会导致db调用吗?有没有办法在一次往返过程中插入具有相关实体的对象