![](/img/trans.png)
[英]EF : How do I convert from Independent Association to Foreign Key Association without changing schema or losing data
[英]How can I do a dependent entity read only without losing the foreign key in EF core 2.0?
我正在嘗試使BlogId和Blog為只讀,而不會將BlogId作為外鍵丟失。 如何在EF Core 2.0中實現這一目標? 謝謝
public class Blog
{
public int Id { get; set; }
public string Url { get; set; }
}
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
private int _blogId;
public int BlogId =>_blogId;
//I want this entity to be read-only without loose the foreign key
//in the database
public Blog Blog { get; set; }
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Property<int>("BlogId").HasField("_blogId");
}
使Blog
屬性的set訪問器protected
public Blog Blog { get; protected set; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.