[英]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.