[英]Prevent one-to-one mapping with Entity Framework Core
我正在重構ASP.NET Core應用程序中的一些EF Core代碼,並且為了使某些應用程序特定的代碼與某些Framework代碼(身份)脫鈎,我想手動管理關系而不是使用EF核心創建一對一映射。
我實質上希望從一個類到另一個類進行引用,這是一對一的映射,但是要使其引用,以使EF Core不會嘗試自動建立這種關系。
所以我有:
public class ApplicationUser : IdentityUser
{
public Author Author { get; set; }
}
和:
public class Author
{
public long Id { get; set; }
public string ApplicationUserId { get; set; }
public ApplicationUser ApplicationUser { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
public string Biography { get; set; }
public virtual ICollection<BlogPost> BlogPosts { get; set; }
}
然后在我的上下文中,這兩個都成為表。 ApplicationUser類用於Identity,另一個表是特定於應用程序的。
EF Core是否有辦法告訴它不要在這些類之間創建一對一映射?
謝謝
您可以使用Ignore()
函數在你的modelBuilder
的覆蓋方法中OnModelCreating()
你的DbContext
是這樣的:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUser >().Ignore(u => u.Author);
modelBuilder.Entity<Author>().Ignore(a => a.ApplicationUser);
//rest of the code
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.