实体框架:多对多关系,插入和更新 [英] Entity Framework : many to many relationship , Insert and update
问题描述
所以在我的上下文中我添加了:
public class Groupnews:DbContext
{
public DbSet< Group>组{get;组; }
public DbSet< News>新闻{get;组;
public Groupnews()
:base(MyDb)
{
}
public int NewsID {get;组;
}
this.HasMany(t => t.News)
.WithMany(t => t.Groups)
.Map(m = &$
{
m.ToTable(GroupNews);
m.MapLeftKey(GroupID);
m.MapRightKey(NewsID);
});
现在我可以使用这种方法获取基于他们的GroupID的消息。但问题是插入新消息和更新。为此,我需要在GroupNews表中保存NewsID和GroupId。做这个在新闻模型中,我定义:
public virtual ICollection< Group>相关组{get;组; }
public News()
{
RelatedGroups = new List< Group>();
}
与组相同:
public virtual ICollection< News>相关新闻组; }
public Group()
{
RelatedNews = new List< News>();
}
在我的新闻控制器中,我添加:
Group group = new Group();
group.RelatedNews.Add(news);
但没有更新,NewsID不会添加到我的GroupNews表。
您不应该单独定义GroupNews。意思是,您不应该在项目中定义一个 GroupNews
类。您必须使用独立关联(新闻
在组
类中列出组
在您的新闻
类)。以下是您的课程应该如下所示:
public class Group
{
...
public Group()
{
this.News = new List< News>();
}
public virtual ICollection< News>新闻{get; set;}
}
public class新闻
{
...
public News()
{
this.Group = new List< Group>();
}
public virtual ICollection< Group>组{get; set;}
}
public class MyContext:DbContext
{
public DbSet< Group>组{get;组; }
public DbSet< News>新闻{get;组; }
}
然后你可以使用 myGroup.News.Add (myNewsItem)
或 myNews.Groups.Add(myGroup)
。实体框架将自动处理插入。请注意,如果要为您的关联启用延迟加载,您应该使用 virtual
关键字。
I have a news entity and I get the news based on their NewsID. then I defined a new entity , a Group and I want to get the news based on their Group ID. I tried to handle this (many to many) relationships using this articleby using code first approach.
so in my context I added :
public class Groupnews : DbContext
{
public DbSet<Group> Group { get; set; }
public DbSet<News> News { get; set; }
public Groupnews()
: base("MyDb")
{
}
public int NewsID { get; set; }
}
this.HasMany(t => t.News)
.WithMany(t => t.Groups)
.Map(m =>
{
m.ToTable("GroupNews");
m.MapLeftKey("GroupID");
m.MapRightKey("NewsID");
});
now I can get the news based on Their GroupID using this approach. but the problem is in insertign new News and updating.For that I need to save NewsID and GroupId in GroupNews table. for doing this . in News model i defined :
public virtual ICollection<Group> RelatedGroups { get; set; }
public News()
{
RelatedGroups = new List<Group>();
}
and the same for group :
public virtual ICollection<News> RelatedNews { get; set; }
public Group()
{
RelatedNews = new List<News>();
}
In my news controller I add :
Group group = new Group();
group.RelatedNews.Add(news);
but nothing is updated and the NewsID is not adding to my GroupNews table .
You should not define GroupNews separately. Meaning, you should not have a GroupNews
class defined in your project. You have to do CRUD operations using independent associations (a list of News
in Group
class and a list of Group
in your News
class). Here's what your classes should look like:
public class Group
{
...
public Group()
{
this.News = new List<News>();
}
public virtual ICollection<News> News {get;set;}
}
public class News
{
...
public News()
{
this.Group = new List<Group>();
}
public virtual ICollection<Group> Groups {get;set;}
}
public class MyContext : DbContext
{
public DbSet<Group> Groups { get; set; }
public DbSet<News> News { get; set; }
}
Then you can use myGroup.News.Add(myNewsItem)
or myNews.Groups.Add(myGroup)
. Entity Framework will handle the insertion automatically. Notice you should use virtual
keyword if you want to enable lazy loading for your associations.
这篇关于实体框架:多对多关系,插入和更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!