实体框架使用相关对象插入对象 [英] Entity Framework Insert object with related object

查看:130
本文介绍了实体框架使用相关对象插入对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新的实体框架,我需要插入一个对象评论有一个相关的FK对象用户进入数据库。

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

public class User
{

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

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

public virtual List< Comment>评论{get;组;
}

public void AddComment()
{
User user = new User(){UserID = 1};
评论comment = new评论(){CommentContent =这是一个评论,CommentCreationTime = DateTime.Now,User = user};

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

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

理想情况下,使用T-SQL,如果我知道我的用户对象,我可以插入我的注释对象,并在insert语句中指定我的'User'的PK。 / p>

我已经尝试对Entity Framework执行相同操作,似乎不起作用。必须首先从数据库中获取 User 对象,才能插入新的Comment。



请问我该如何实现?

解决方案

将用户对象附加到上下文,以便上下文知道其现有实体

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

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

ctx.Users.Attach(user);

评论评论=新评论(){CommentContent =这是评论,CommentCreationTime = DateTime.Now,User = user};

comments = new CommentsRepository(ctx);

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


I am a newbie with Entity Framework and I need to insert an object Comment that has a related FK object User into the database.

    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();
   }

Ideally, with T-SQL, if I know the PRIMARY KEY of my User object, I could just insert my Comment object and specify the PK of my 'User' in the insert statement.

I have tried to do the same with Entity Framework and it doesn't seem to work. It would be overkill to have to first fetch the User object from the database just to insert a new 'Comment'.

Please, how can I achieve this ?

解决方案

You need to attach the user object to the context so that the context knows its an existing entity

  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();
   }

这篇关于实体框架使用相关对象插入对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆