繁体   English   中英

如何使从属实体只读而不丢失EF core 2.0中的外键?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM