ErikEJ / EntityFramework6PowerTools

This is the codebase for Entity Framework 6 Power Tools Community Edition, and modern EF 6 providers for SQL Server and SQL Server Compact
Other
183 stars 27 forks source link

Defaults not set in Table #87

Closed LeopoldLerch closed 2 years ago

LeopoldLerch commented 2 years ago

Hi, I am using EF6 in Code-First. I have the strange problem, that obviously defaults are not set in tables when the migration is applied using your Nuget-Package ErikEJ.EntityFramework.SqlServer.

I use it by setting DbConfiguration.SetConfiguration(new MicrosoftSqlDbConfiguration()); in the configure-Method in Startup. Authorization etc works fine (i am using Azure integrated authentication)

When checking the create-Statements of the created tables for example the following is missing:

ALTER TABLE [dbo].[MLRegistration] ADD  DEFAULT (getdate()) FOR [RegistrationTime]
GO

When creating the same table using another project which is not using your nuget-package (or removing it in the very same project), this line is there (and the defaults are applied).

The Migration-Code which should be applied looks like that:

CreateTable( "dbo.MLRegistration", c => new { UserName = c.String(nullable: false, maxLength: 128), Disabled = c.Boolean(nullable: false), RegistrationTime = c.DateTime(nullable: false, annotations: new Dictionary<string, AnnotationValues> { { "SqlDefaultValue", new AnnotationValues(oldValue: null, newValue: "getdate()") }, }), }) .PrimaryKey(t => t.UserName);

Maybe you can help me?

ErikEJ commented 2 years ago

Limited rights to the database?

I probably need a repro project.

LeopoldLerch commented 2 years ago

No, it is the same database, tried even with the same connectionstring (with user/password authentication)

When simply commenting out DbConfiguration.SetConfiguration(new MicrosoftSqlDbConfiguration())

the table is set up with correct defaults. When deleting the migration from history and the tables, remove the comment from the config above and rerunning it, the table is created also, but the defaults are not set.

Not limited to one table, i have 2 tables with the same issue.

ErikEJ commented 2 years ago

I need a simple repro.

LeopoldLerch commented 2 years ago

Will a Model help, from which you then can generate a migration for your database?

ErikEJ commented 2 years ago

Sure, an console app with just the model.

LeopoldLerch commented 2 years ago

EFTestApp.zip

You will need to insert your own connectionString of course. When commenting in the DBConfiguration-Line, the Defaults for the DateTime-Columns are not set. When commented out, they are correct.

Thanks

LeopoldLerch commented 2 years ago

Okay, seems to have something todo with SetSqlGenerator("System.Data.SqlClient", new DefaultValueSqlServerMigrationSqlGenerator()); in Configuration.cs of my example.

If i Change that to "Microsoft.Data.SqlClient", it seems to work.

However, I have the DatabaseModel separated in a libary, as it is used by more projects. And only one of them is using your package as it is the only one on .Net Core. So I cannot change this value for all of them. Is there any way to get that part somehow flexible?

ErikEJ commented 2 years ago

Sure, create a seperate .csproj/package and use conditional compilation.

CZEMacLeod commented 2 years ago

@LeopoldLerch Pretty sure, as it is just a string value, you can register both and it will pickup based on the provider in use.

SetSqlGenerator("System.Data.SqlClient", new DefaultValueSqlServerMigrationSqlGenerator());
SetSqlGenerator("Microsoft.Data.SqlClient", new DefaultValueSqlServerMigrationSqlGenerator());

I have something very similar to the DefaultValueSqlServerMigrationSqlGenerator in my own code, although I have extension methods in the same library that sets up my DbMigrationsConfiguration and DBContexts which I call in the constructors. This means that I should be able to just add the line to my extension method and all my libraries will be enabled for both providers.

LeopoldLerch commented 2 years ago

Sure that i can Register both? Dont they Override each other?

Will try in monday. Thx

Cynthia MacLeod @.***> schrieb am Fr., 1. Okt. 2021, 17:47:

@LeopoldLerch https://github.com/LeopoldLerch Pretty sure, as it is just a string value, you can register both and it will pickup based on the provider in use.

SetSqlGenerator("System.Data.SqlClient", new DefaultValueSqlServerMigrationSqlGenerator());SetSqlGenerator("Microsoft.Data.SqlClient", new DefaultValueSqlServerMigrationSqlGenerator());

I have something very similar to the DefaultValueSqlServerMigrationSqlGenerator in my own code, although I have extension methods in the same library that sets up my DbMigrationsConfiguration and DBContexts which I call in the constructors. This means that I should be able to just add the line to my extension method and all my libraries will be enabled for both providers.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/ErikEJ/EntityFramework6PowerTools/issues/87#issuecomment-932344208, or unsubscribe https://github.com/notifications/unsubscribe-auth/AG4MXZZA6O6TS7LCSUYXHK3UEXJYJANCNFSM5FEU4F5A . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.