entity framework删除外键表中数据
我采用code frist方式:
User表
Role表
UsersInRoles表
User:
public class User
{
public string UserId { get; set; }
public string UserName { get; set; }
public virtual ICollection<User> Roles { get; set; }
}
Role:
public class Role
{
public string RoleId { get; set; }
public string RoleName { get; set; }
public virtual ICollection<Role> Roles { get; set; }
}
public class DbEntities : DbContext
{
public IDbSet<User> Users { get; set; }
public IDbSet<Role> Roles { get; set; }
modelBuilder.Entity<User>().Property(i => i.UserId)
.HasDatabaseGen eratedOption(DatabaseGeneratedOption.None);
modelBuilder.Entity<User>()
.HasMany(u=>u.Roles)
.WithMany(u=>u.Users)
.Map(m =>
{
m.ToTable("UsersInRoles");
m.MapLeftKey("UserId");
m.MapRightKey("RoleId");
});
}
现在我想删除UsersInRoles中数据,如何删除
------解决方案--------------------
直接使用UsersInRoles实体对象的Delete方法就可以了