Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion for documentation - Partial classes for your dbContext #3406

Closed
ChristineBoersen opened this issue Oct 12, 2015 · 4 comments
Closed

Comments

@ChristineBoersen
Copy link

Something you may want to recommend in the documentation, especially for complex data models, is using a Partial Class for you dbContext class.

By using a pattern such as this, you can keep all the builder logic for your model with your POCO so it is easier to work on in a team environment (low concurrency issue with your MyContext.cs file).

In file Address.cs

public class Address
{

    public Address()
    {

        BusinessAddresses = new HashSet<BusinessAddress>();
        PersonAddresses = new HashSet<PersonAddress>();
        TransactionAddresses = new HashSet<TransactionAddress>();

    }

    public Int64 AddressID { get; set; }
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string PostalCode { get; set; }
    public string County { get; set; }
    public string Country { get; set; }

    public bool Active { get; set; }
    [Timestamp]
    public byte[] RowVersion { get; set; }

    public virtual ICollection<BusinessAddress> BusinessAddresses { get; set; }
    public virtual ICollection<PersonAddress> PersonAddresses { get; set; }
    public virtual ICollection<TransactionAddress> TransactionAddresses { get; set; }


}

public partial class MyContext
{
    public virtual DbSet<Address> Address { get; set; }

    protected void Address_CreateObject()
    {
        _ModelBuilder.Entity<Address>(entity =>
        {

            entity.Key(e => e.AddressID).SqlServerClustered();
            entity.Property(e => e.AddressID).UseSqlServerIdentityColumn();

            // Put your fields here for any additional building



            entity.Property(e => e.Active).Required().HasSqlServerDefaultValue(true);

            entity.Property(e => e.RowVersion).ConcurrencyToken();



        });

    }

}

And then in your "main" dbContext partial class, you just add the one line of code, Address_CreateObject() in this case for it to call your builder code in the Address.cs file's MyContext partial class.

MyContext.cs

public partial class MyContext: dbContext
{

    protected ModelBuilder _ModelBuilder { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        _ModelBuilder = modelBuilder;      

        Address_CreateObject();

        base.OnModelCreating(modelBuilder);


    }


}
@Maverik
Copy link

Maverik commented Oct 12, 2015

Nice suggestion. I was wondering how to do this and now I see what I was missing. Thanks!

@ChristineBoersen
Copy link
Author

Glad I could help, I hope the idea helps others as well :)

@rowanmiller
Copy link
Contributor

Partial classes is one way to do this. Another option is just having static methods that they can call from within OnModelCreating. Ultimately we are going to enable a pattern like the EntityTypeConfiguration<T> from EF6 (tracked by #2805).

Bottom line is that there are lots of ways to organize code and we don't think we need specific docs about how to organize the code in OnModelCreating.

@rebeccapowell
Copy link

What we be really useful would be to make void OnModelCreating(ModelBuilder modelBuilder) a partial method.

If you are doing DbFirst and recreating the DbContext, it means that any custom OnModelCreating code can be split into a partial class / partial method and the code added to the partial method won't be lost. Currently, after rescaffolding you lose anything custom inside the OnModelCreating override to the underlying DbContext OnModelCreating.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants