-
Notifications
You must be signed in to change notification settings - Fork 821
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
Support for EFMigration and Database creation #91
Conversation
I'm working with classic BloggingContex example with classes Blog and Post... This is how my app.config looks like: <?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<entityFramework>
<providers>
<provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql.EntityFramework"></provider>
</providers>
<defaultConnectionFactory type="Npgsql.NpgsqlFactory, Npgsql" />
</entityFramework>
<connectionStrings>
<add name="BloggingContext" providerName="Npgsql" connectionString="server=localhost;port=5432;userid=npgsql_tests;password=npgsql_tests;database=ef6_code_first_sample" />
</connectionStrings>
<system.data>
<DbProviderFactories>
<remove invariant="Npgsql" />
<add name="Npgsql Data Provider" invariant="Npgsql" support="FF" description=".Net Framework Data Provider for Postgresql" type="Npgsql.NpgsqlFactory, Npgsql" />
</DbProviderFactories>
</system.data>
</configuration> |
Hi, David! Thank you for your pull request! This will be a great addition to Npgsql EF support. I'll check it and if I have any problems I'll contact you. Then I'll merge it in the current master branch! |
sql.Append("\" "); | ||
AppendColumnType(column, sql, true); | ||
|
||
if (column.IsNullable ?? false) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this test is backwards.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you think that if IsNullable is not set should mean that column has to be NOT NULL? I think that if is not set it should be nullable...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think "NOT NULL" should only be added if IsNullable has a value and it's value is false.
You're adding NOT NULL when IsNullable is true.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if(column.IsNullable != null && !column.IsNullable) {...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you. Feeling much stupid atm :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could also simply do if(column.IsNullable == false)
.
Excellent! I tested it here and it worked perfectly! Do you think it would be possible to add some unit tests to the NpgsqlMigrationSqlGenerator class? Maybe adding some handcrafted migrationOperations ? I mean, we could create some fake migrationOperations and compare the expected command creation with the actual commands created? This way we wouldn't need to create a full blown EF project but we still could test the command generation correctness? What do you think? |
I might do UnitTests this weekend if i get some time ;)
One is faster other is more effective in my opinion... |
We should go with the second option. I agree with you that it is more effective. |
Another detail I noticed is that those changes fail to build in the Npgsql project for visual studio 2013 when using the net35 configuration. This is the reported error: [Csc] Npgsql\NpgsqlServices.cs(113, 33): error CS0115: 'Npgsql.NpgsqlServices.DbDatabaseExists(System.Data.Common.DbConnection, int?, System.Data.Metadata.Edm.StoreItemCollection)': no suitable method found to override I tested it here and I think you can wrap this code with a #ifdef NET40 letting the UsingPostgresDBConnection out of the #if. |
private void CreateExtension(string exensionName) | ||
{ | ||
//This is compatible only with server 9.1+ | ||
if (serverVersion.Major > 9 || (serverVersion.Major == 9 && serverVersion.Minor >= 1)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If someone will work on this code notice that extension like UUID will not be created on older server versions...
Hi, David! We merged pull request #110 which made your pull request not apply cleanly anymore. Would you mind to rebase it on current master? Thanks in advance. |
I reset my Master to upstream Master and added NpgsqlMigrationSqlGenerator.cs only in Npgsql.EntityFramework project(since this is only EF6+ working code(MigrationSqlGenerator had some changes between EF4 and EF6)). I will now start looking into creating UnitTests... |
Excellent, David! Your pull request is one I'd like to include in next 2.1 release before we I'm looking forward your tests. I could only test the db creation in my
|
It seems like current NpgsqlTests project does not have EntityFramework included... Will you guys create NpgsqlTests.EntityFramework project which will have references to Npgsql.EntityFramework and EntityFramework nuget package? |
@DavidKarlas, there is no reason not to add a project reference from NpgsqlTests to Npgsql.EntityFramework (and even to Npgsql.EntityFrameworkLegacy), no need for extra projects... Go ahead and do so in your pull request, |
Hi David. I have pull request #119 for updating Npgsql's provider manifest. It will affect EF support. I have watched your changes. I think your patch won't be affected. If you can prepare unit tests for your pull request, I'm willing to test it with my patch. :) |
To be honest I'm not 100% what #119 is doing. But I think there won't be problems. I was working on how to make unit tests for this migrations during weekend. And I will probably make one or two tests against DB and rest will be string compare based testing because It's hard to create migrations test because have to modify classes and then run AddMigration in Nuget console...(this sounded simple but It's nasty) |
I also think there won't be problems.
Ok. I think string comparison will work too. |
@DavidKarlas, don't mean to press but have you had any time to work on the unit tests? I'm only asking because we're waiting on this pull request to start the 2.1 release cycle |
I will do it this weekend ;) Hope it's not too late... |
Thanks :) of course it isn't too late... |
I think we really should be freezing version 2.1 soon, and are waiting only for this. I'd say if you can't get this done this weekend, we can leave it for the next release, ok? |
Sorry for delaying this for one week but could we skip this feature for 2.1 release because I done only few tests so far plus I'm not happy about only supporting server 9.3(because CREATE SCHEMA IF NOT EXISTS)... Plus there are probably some bugs... Plus it seems like this not very demending feature atm... |
No problem @DavidKarlas, if you're not 100% happy with it, take your time and work on it for the next release... |
I'm still alive :) I was hyped on NetDuino and some other stuff... |
Welcome back! :)
Excellent!! I'll have a look at this EF bug. Please, let us know if you have any problems merging your code back to Npgsql code base. We made a lot of changes. Thanks in advance. |
OK. So if the npgsql_tests user has to have database create/drop permissions for the EF tests, we might as well make it create the non-EF npgsql_tests database as well like @DavidKarlas suggested... |
On Wed, Feb 12, 2014 at 3:38 PM, Shay Rojansky notifications@github.comwrote:
Agreed! +1
Regards, Francisco Figueiredo Jr. |
Shay, do you think it would be too much annoying to raise the requirements On Wed, Feb 12, 2014 at 4:22 PM, Francisco Figueiredo Jr. <
Regards, Francisco Figueiredo Jr. |
Not at all, Francisco, I think it's OK... It's only developers who run the unit tests and it's a reasonable requirement, especially since the tests actually test createdb capabilities... I'd say go for it. |
Hi, @DavidKarlas ! I'm just configuring our database server to give permission to our tests user to create database so those new tests don't raise errors. As soon as I get that resolved, I'll merge your code. I think it is very good and will be an excellent addition to EF users! Thank you very much! |
Glad to hear that ;) I will keep watching issues on Github and resolve any problems with migrations ASAP when users find them. I hope you put some -Pre Nuget binary on soon :) |
Great! I'll also write a blog post about this new feature so more people will start to use and test it! :) |
A small note... Right now this PR doesn't compile for me (and the build server) because references point to EF6.0.2, but in the packages directory I can see EF6.0.1. Can you confirm that you've switched to nuget "package restore" and this is why I'm not seeing EF6.0.2 in git? If so, the old EF6.0.1 needs to be deleted and I will enable package restore on our build server. |
Yes I switched to EF6.0.2.
|
Yeah, unless you have a specific need for EF6.0.2 right now, please switch back to EF6.0.1 for now. After the PR is merged I'll take care of moving to EF6.0.2 with clean nuget package restore etc. Thanks. |
I also think it is better to use the 6.0.1 version. |
Is this build server of yours a bit lazy? :P |
On Mon, Feb 17, 2014 at 6:08 PM, David Karlaš notifications@github.comwrote:
:D We didn't enable automatic build of pull requests yet. =) Only merges are automatically being built.
|
Hi, @DavidKarlas ! I'm still having problems compiling your changes even after you reverted to use EF 6.0.1. I'm checking what is happening and I will merge your changes when I get the builds going. Sorry for delay... :( |
I think I got it! I tried a custom build where I do a clean checkout before the build and now it is compiling! It was giving errors when trying to compile the EntityFrameworkTests as it couldn't find the EntityFramework.dll assembly. |
Everything is almost ready. Compilation problem is now solved. Now I need to fix the permission of npgsql_tests user to be able to create database. I forgot to make this configuration change :) Right now I'm at my job and the firewall here doesn't allow me connect to the build server to do this change. I'll make it later and then I hope everything will be ready to merge! :) |
Glad to hear this! |
Hi, @DavidKarlas ! I just added the permission to create database to our tests user. Create database tests and a lot of others are passing ok.
I think server version checks need to be made inside the tests too just like the creation statements. :) Those errors appear on all server versions tested (8.4, 9.0, 9.1 and 9.2) but 9.3 What do you think? Other than that, everything seems to be going very well. :) |
Hi, @DavidKarlas ! |
We are almost there! On .net 4.0 and 4.5 all tests pass ok. EntityFrameworkBasicTests are failing on .net 3.5 and 2.0:
I think BasicTests are missing the #if NET40 like you did with MigrationTests. |
Excellent work, David! All tests pass now! Merging now... |
Support for EFMigration and Database creation
I hope I am not asking in the wrong place. I have built the master branch so that I could begin to use EF Migration support (excellent work BTW). Two things that I have noticed, but I'm not sure if I am doing something wrong. First, all of my string entity properties are being rendered (when doing add-migration) as
which causes PostgreSQL/npgsql to throw an exception. I would like them to be either:
or
Is there any way to default it to one of the above methods when doing an add-migration? Second, it appears when one table depends upon another, in my example I have a "country" table and a "user" table that has a foreign key to "country.id". Again, when doing add-migration the migration works very well, but the tables being depended upon are listed after the tables that depend. In my example, the user table CreateTable is being called before the country CreateTable. It is easy to rearrange the order, but I was wondering if this was expected or if it is something that can be fixed? |
End goal here is that you don't care if you are targeting MS SQL or Postgresql so EVERYTHING must be fixed :) (Except stored procedures) I would prefer if you could open two separate issues for this for better tracking and stuff... It would also be awesome if you could copy-paste output of "Update-Database –Verbose" command where actual Postgresql commands are displayed ;) Btw tnx for giving it a try ;) |
Hi, I am trying to create an MVC web site using PostGreSQL with PostGIS. I simply can't get this configured and it's riving me slowly insane. I get the error: Your project references the latest version of Entity Framework; however, an Entity Framework 3 database provider compatible with this version could not be found for your data connection. If you have already installed a compatible provider, ensure you have rebuilt your project before performing this action. Otherwise, exit this wizard, install a compatible provider, and rebuild your project before performing this action. No matter what I do, after following all of the advice here, and obviously need an EDM to be able to autocomplete from the database. Using it with a connection string allows me to easily connect to teh database and select and display data, but obviously I want to use models and the EDM. Any ideas? I'm rapidly balding here... |
After following this guide for EntityFramework http://fxjr.blogspot.com/2013/06/npgsql-code-first-entity-framework-431.html I realized there is no support for automatic creation of database and tables... So i created support for DatabaseExist, DatabaseDelete and DatabaseCreate in NpgsqlServices.cs. Also added NpgsqlMigrationSqlGenerator in constructor so it's loaded by dependency injector...
Wrote NpgsqlMigrationSqlGenerator which does not handle Procedures and switching type from int to serial(doesn't add default value to new sequence...) plus I'm not sure how well Indexes work...
I will soon start using this migration on project I'm starting so I'm making this pull request to make people aware of it if someone wants to play with it. You could also merge it it effects only #if ENTITIES6...
My point is... its not complete yet...