0
votes

I have existing database. Then new requirement came for more tables so I created model and used code first approach to generate tables to existing db.

But when I run command

Enable-Migrations -ContextTypeName ListHell.CODE.flagen -force

I get error

Creating a DbModelBuilder or writing the EDMX from a DbContext created using Database First or Model First is not supported. EDMX can only be obtained from a Code First DbContext created without using an existing DbCompiledModel.

I have checked all the answer related to it on SO but all are useless to my case

my dbcontext class

  public partial class flagen:DbContext
{
    public flagen() : base("name=LH_newEntities")
    {
    }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    public virtual DbSet<flag> flags { get; set; }
}

thanksfor help

1
What answers? Why are they useless? If the error message is accurate there is no other answer to give. Have you mixed Db-First and Code-First contexts in the same project perhaps? - Panagiotis Kanavos
@PanagiotisKanavos one answer says delete the _migrationhistory and delete the migraition folder then try again I did that no result. other is exactly the same. - user786
The error is about model-first contexts. Not migrationhistory. Do you have model-first or db-first contexts? Is the error accurate? Why is your class partial? - Panagiotis Kanavos
@PanagiotisKanavos I have existing database and doin code first its in the question - user786
I'll bet that flagen is also a db-first context because it has the partial keyword - Panagiotis Kanavos

1 Answers

0
votes

I was sharing the connection string of codefirst and dbfirst. it turned out this will not work. I created new connection string and its works smoothly. Solved