[英]How to add an item to a many-to-many relationship table in Entity Framework Core?
In my EF Core data model I have two table, the first table have an ICollection
Navigation property to the second table and the second table also have an ICollection
Navigation property to the first table. 在我的EF Core数据模型中,我有两个表,第一个表的第二个表具有
ICollection
Navigation属性,第二个表的第一个表也具有ICollection
Navigation属性。
public class MyUser
{
[Key]
public string UserName { get; set; }
public ICollection<Vote> Votes { get; set; }
public virtual ICollection<PrivateUser> Rooms { get; set; }
}
public class PrivateUser
{
[Key]
public string RoomName { get; set; }
public virtual ICollection<MyUser> Users { get; set; }
}
When I first add a MyUser
only with name, 当我第一次添加仅具有名称的
MyUser
时,
appuser = new MyUser
{
UserName = Context.User.Identity.Name
};
_context.MyUser.Add(appuser);
_context.SaveChanges();
it's adding the user to the table correctly but when I try add a PrivateUser
with an existing MyUser
or I try to update the existing MyUser
it gives NullReferenceException
from the below code. 它正确地将用户添加到表,但是当我尝试添加
PrivateUser
与现有MyUser
或我尝试更新现有MyUser
它给人NullReferenceException
从下面的代码。
PrivateUser roma = new PrivateUser() { RoomName = roomName};
try
{
var user = new MyUser() { UserName = Context.User.Identity.Name };
user.Rooms.Add(roma);
_context.MyUsers.Attach(user).Property(u => u.Rooms).IsModified = true;
roma.Users.Add(user);
await _context.Rooms.AddAsync(roma);
await _context.SaveChangesAsync();
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
I've also configured it in the ModelBuilder. 我还在ModelBuilder中配置了它。
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<MyUser>().HasMany(u => u.Rooms);
modelBuilder.Entity<PrivateUser>().HasMany(c => c.Users);
}
I'm a beginner on EF Core, can somebody please explain me how I can save the fields in correct way when the both table referencing themselves. 我是EF Core的初学者,有人可以向我解释当两个表都引用自己时如何以正确的方式保存字段。
After a long investigate I found there was no problem with my Entity Model the problem was because I solved that problem on my first Migration
by using ModelBuilder
经过长时间的调查,我发现实体模型没有问题,原因是因为我在第一次
Migration
使用ModelBuilder
解决了该问题
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<MyUser>().HasMany(u => u.Rooms);
modelBuilder.Entity<PrivateUser>().HasMany(c => c.Users);
}
The problem was in my try
block, see the correct code below. 问题出在我的
try
块中,请参见下面的正确代码。
try
{
var user = new MyUser() { UserName = Context.User.Identity.Name };
//here I was doing user.Rooms.Add(roma); where Romms is null
roma.Rooms = new ICollection<PrivateUser>();
//now we can add the room to Rooms
user.Rooms.Add(roma);
//and here was the below commented code from a Stackoverflow Answer
//_context.MyUsers.Attach(user).Property(u => u.Rooms).IsModified = true;
//we just have to use Update here;
_context.MyUsers.Attach(user);
_context.MyUsers.Update(user);
roma.Users.Add(user);
await _context.Rooms.AddAsync(roma);
await _context.SaveChangesAsync();
}
Now everything fine as expected. 现在一切都按预期进行。 Thanks...
谢谢...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.