1

I use entity framework 6.1.3 model first approach. I needed to design a simple model as following.

Then i generated sql to create the database but i found that my entities sets were not defined in my DbContext. I tried to add them manually but Entity Framework keeps regenerating the following code. It only generates the abstract entity which is exactly the opposite of what it is designed for.

public partial class UploadsDataModelContainer : DbContext
{
    public UploadsDataModelContainer()
        : base("name=UploadsDataModelContainer")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }
    public virtual DbSet<AzureBlobs> AzureBlobs { get; set; }
}

Is this a bug in EF code generator or am i missing a step in model creation ?

4
  • You probably want to map both subtypes to their own tables. See TPH vs. TPT or TPC inheritance. And add an entity set name in the designer. Commented Mar 27, 2017 at 15:16
  • If you want, because Visual Studio generates partial classes, you can add a new file with the same UploadsDataModelContainer context and add the relevant properties: public DbSet<Upload> Uploads { get; set; } Commented Mar 27, 2017 at 15:22
  • @RicardoPeres That's true i will try it out and give you feedback. Thank you. Commented Mar 27, 2017 at 15:30
  • 1
    @RicardoPeres What you've suggested worked well for me. You saved my time man. Commented Mar 27, 2017 at 16:33

1 Answer 1

1

Thanks to RicardoPeres i added a class file with the following content and it resolved my problem by avoiding the automatic updates of the DbContext.cs file :

    public partial class UploadsDataModelContainer : DbContext
{

    public virtual DbSet<Chunk> CHunks { get; set; }
    public virtual DbSet<Upload> Uploads { get; set; }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.