实体框架 - 命名“自动生成” InverseProperty表 [英] Entity Framework - Naming a "autogenerated" table on InverseProperty
问题描述
我做了一些搜索,但找不到同样的问题/问题。
当EF创建表遇到InverseProperty DataAnnotation
Dummy / Demo code时,我有一个关于表命名(在数据库中)的问题:
public class User
{
public Guid Id {get;组; }
public String Name {get;组; }
public virtual List< Event> InvitedEvents {get; set;}
public virtual List< Event> AcceptedEvents {get; set;}
public virtual List< Event> DeclinedEvents {get; set;}
}
public class Event
{
public String Name {get; set;}
[InverseProperty(InvitedEvents)]
public virtual List< User> InvitedUsers {get;组; }
[InverseProperty(AcceptedEvents)]
public virtual List< User> AcceptedUsers {get;组; }
[InverseProperty(DeclinedEvents)]
public virtual List< User> DeclinedUsers {get;组; }
}
这一切都起作用,查找表创建,但我的问题是
我如何给这个表一个名字?
他们现在被命名像$)
- EventUser
- EventUser_1
- EventUser_2
我想给它一些更可读的名称。 / p>
希望很清楚。
更新!
正如 Ladislav Mrnka 回答的那样,流畅的API是解决方案。我删除了[InverseProperty]属性,并将创建的关系添加到DataContext类。
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity< Event>()
.HasMany(e => e.InvitedEvents)
.WithMany(u => u.InvitedUsers)
.Map(mc =>
{
mc.ToTable(%TABLENAME%);
mc.MapLeftKey(UniqueId);
mc.MapRightKey( UniqueId);
});
}
您需要确保每个类都包含一个UniqueId列(在我的演示中例如EventId,UserId)
答案也在回答中更新
如Ladislav Mrnka的回答,流利的API是解决方案。我删除了[InverseProperty]属性,并将创建的关系添加到DataContext类。
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity< Event>()
.HasMany(e => e.InvitedEvents)
.WithMany(u => u.InvitedUsers)
.Map(mc =>
{
mc.ToTable(%TABLENAME%);
mc.MapLeftKey(UniqueId);
mc.MapRightKey( UniqueId);
});
}
I did a bit of searching but can't find a same problem / question. I have a question regarding the table naming (in the database) when EF creates a table encountering InverseProperty DataAnnotation
"Dummy/Demo code" :
public class User
{
public Guid Id { get; set; }
public String Name { get; set; }
public virtual List<Event> InvitedEvents { get; set;}
public virtual List<Event> AcceptedEvents { get; set;}
public virtual List<Event> DeclinedEvents { get; set;}
}
public class Event
{
public String Name { get; set;}
[InverseProperty("InvitedEvents")]
public virtual List<User> InvitedUsers { get; set; }
[InverseProperty("AcceptedEvents")]
public virtual List<User> AcceptedUsers { get; set; }
[InverseProperty("DeclinedEvents")]
public virtual List<User> DeclinedUsers { get; set; }
}
This all works and the lookup tables is created, but my question is How can i give that table a name ?
They are now named (something like) - EventUser - EventUser_1 - EventUser_2
I want to give some more "readable" names to it.
Hope it's clear.
UPDATE!
As answered by Ladislav Mrnka, fluent API was the solution. I removed the [InverseProperty] properties and added the creation of relations to the DataContext class.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Event>()
.HasMany(e => e.InvitedEvents )
.WithMany(u => u.InvitedUsers )
.Map(mc =>
{
mc.ToTable("%TABLENAME%");
mc.MapLeftKey("UniqueId");
mc.MapRightKey("UniqueId");
});
}
You need to make sure that each class contains a UniqueId column (in my demo example EventId, UserId)
The answer also updated in answer
As answered by Ladislav Mrnka, fluent API was the solution. I removed the [InverseProperty] properties and added the creation of relations to the DataContext class.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Event>()
.HasMany(e => e.InvitedEvents )
.WithMany(u => u.InvitedUsers )
.Map(mc =>
{
mc.ToTable("%TABLENAME%");
mc.MapLeftKey("UniqueId");
mc.MapRightKey("UniqueId");
});
}
这篇关于实体框架 - 命名“自动生成” InverseProperty表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!