dotnet / EntityFramework.Docs

Documentation for Entity Framework Core and Entity Framework 6
https://docs.microsoft.com/ef/
Creative Commons Attribution 4.0 International
1.62k stars 1.96k forks source link

Please, add example with model property that have setter only. #1077

Open DeveloperLookBook opened 5 years ago

DeveloperLookBook commented 5 years ago

Please, show how to configure model properties that have setter only.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

roji commented 4 years ago

See the following sample for a write-only property. I'm not sure this scenario is widely-used enough so as to justify being mentioned in our main docs (@ajcvickers?) - every added bit makes the docs longer and heavier.

public class BlogContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder.UseSqlServer(...);

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Blog>()
            .Property("WriteOnlyProperty")
            .HasField("_writeOnlyField");
    }
}

public class Blog
{
    private string _writeOnlyField;
    public int Id { get; set; }

    public string WriteOnlyProperty
    {
        set => _writeOnlyField = value;
    }
}
ajcvickers commented 4 years ago

@roji Variations on this theme are not uncommon. I think it's worth documenting some of the patterns that work and the limitations that they have. It's not high priority though.