zzzprojects / EntityFramework-Classic

Entity Framework Classic is a supported version of the latest EF6 codebase. It supports .NET Framework and .NET Core and overcomes some EF limitations by adding tons of must-haves built-in features.
https://entityframework-classic.net
Other
103 stars 27 forks source link

Effort with DbFirst model on .Net Framework 4.0 or newer seems unusable or not documented #5

Closed we-apon closed 5 years ago

we-apon commented 5 years ago

Hi. Sory for bad english and maybe for issue on wrong project.

I've trying to use Z.EntityFramwrodk.Classic.Effort in test project with an DbFirst model and <TargetFramework>net462</TargetFramework>, but can't get any luck with it. Once DbContext created - it goes with CodeFirst way and throws standard UnintentionalCodeFirstException.

I've tried it as in documentation example, like:

var connection = Effort.DbConnectionFactory.CreateTransient();
var context = new EntityContext(connection));

But, at least in documentation, there is no any way to specify some *.edmx model. It seems to me - there need to call EntityFrameworkManager.UseDatabaseFirst("SomeModel.edmx") like in .netstandard2.0 target, but this method is missed in net45 target library of Z.EntityFramework.Classic

JonathanMagnan commented 5 years ago

Hello @we-apon ,

I might be wrong but I believe you need to comment the UnintentionalCodeFirstException

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    //throw new UnintentionalCodeFirstException();
}

It's true that you are database first, but Effort is a memory provider and table/relation must be created as Code First does.

Let me know if that successfully answer your question

Best Regards,

Jonathan

we-apon commented 5 years ago

@JonathanMagnan thanks for answer.

Well, it works, if comment exception.. kind of.

At first - it wants some [key] attrubute on primary key columns of tables, there key column not named like Id or EntityId, or key is composite.

And also, my model contains couple of classifier- tables with Integer Ids. EF, by default, treats integer Id as autoincrement column with identity(1, 1), so constant Ids on that tables are transformed into sequential numbers on inserts. This maybe my mistake to use primary keys as constants.

It's easy to add all needed attributes through T4 template, but it seems kind a mess. It will become CodeFirst model, created through ModelFirst approach.

But, for now it works, so really thank you.)

JonathanMagnan commented 5 years ago

Yup that will be a mess ;)

I would recommend you to use fluent API instead to set everything instead of DataAnnotations

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<KeyValuePair>().HasKey(x => x.Key);
    base.OnModelCreating(modelBuilder, null);
}

So you will only affect Effort and never the model generated.

we-apon commented 5 years ago

Yeah) My bad, I forgot about that fluent api still hate code first after two years with nHibernate

JonathanMagnan commented 5 years ago

lol ;)

I love code first for quick testing or debug such as our case when someone submits a bug.

But I always prefer to create the database myself for real application.

I will close this request since it seems answered but feel free to create a new one if you need more information.

Best Regards,

Jonathan