我的项目中有用户模型和事件模型。事件具有创建者(用户)并具有参与者(用户),因此事件与用户具有一对多关系,并且与同一个表具有多对多关系。
我首先是这样的一对多关系:
Public class Event
{
...
public int CreatedById { get; set; }
public virtual User CreatedBy { get; set; }
...
}
然后当我添加多对多关系时,迁移不会产生多对多的关系:
Public class User
{
...
public virtual ICollection<Event> Events { get; set; }
...
}
Public class Event
{
...
public int CreatedById { get; set; }
public virtual User CreatedBy { get; set; }
public virtual ICollection<User> Users { get; set; }
...
}
如果我删除了一对多关系,那么迁移会成功生成多对多关系。
有没有办法只用数据注释来做到这一点?
答案 0 :(得分:15)
EF不知道User.Events
必须映射到哪里。它可以是Event.CreatedBy
,也可以是Event.Users
。两者都会产生有效的模型。您必须通过应用[InverseProperty]
属性
public class User
{
...
[InverseProperty("Users")]
public virtual ICollection<Event> Events { get; set; }
...
}
答案 1 :(得分:8)
使用Code First Approach,我总是建议使用流畅的API而不是使用DataAnnotations,它会自动使用一些转换。
这样,您就会知道自己的确切配置。
如果我是你,这就是我要用的:
public class EventMap : EntityTypeConfiguration<Event>
{
public EventMap()
{
this.HasRequired(m => m.CreatedBy) // envent must have a creator
.WithMany() // a user can have 0,1 or more events created by him
.HasForeignKey(m => m.CreatedById) // specify property to be used as FK
.WillCascadeOnDelete(true); // delete all events created by user if that specific user is deleted
this.HasMany(m=>m.Users) // an event can have 0,1 or more participants
.WithMany(m=>m.Events) // a user can be a participant in 0,1 or more events
.Map(m => m.MapLeftKey("EventId").MapRightKey("UserId")); // this will generate intermediate table to hold participant information - dbo.EventUser with EventId & UserId
// Cascade Delete is always true for Many to Many mapping. however, it doesn't delete entry in other table, it deletes entry in Joined Table only.
}
}