How to set the Unique Key constraint in Entity Framework Code First using Configuration or Fluent API.
As a workaround (while they implement the feature Joachim's pointing at), you can create a unique index in a migration.
I would create an "empty" migration and, in the generated class, add the index with CreateIndex in the Up method and drop it with DropIndex in the Down method
Related
In a netstandard20 project, I'm using Npgsql.EntityFrameworkCore.PostgreSQL 3.1.0 and I'm having an issue using Guid as the type for my primary keys. If I use Database.EnsureCreated() to create the schema, the primary key columns get created with data type uuid. With everything the same except changing it to use Database.Migrate() creates all the primary key columns with data type text and then I get exception: Npgsql.PostgresException : 42883: operator does not exist: text = uuid when I try to perform any operations.
There is a single migration in my project to create the initial database. In that migration and also in the "context model snapshot" that are auto-generated, the primary key columns are all defined as Guid.
I need to both use the Database.Migrate() method to create the database and also have it create the Guid primary key columns as uuid. How can I make sure it creates the columns correctly?
I found this issue and response after posting my question. It led me to the correct answer which was that when I created the migration I was using sqlite. I needed to blow away the migration created against that data provider and regenerate it.
Can anyone explain what's the conceptual difference between #UniqueEntity validator, #UniqueConstraint table annotation and unique=true option of #Column annotation.
I understand that #UniqueConstraint adds UNIQUE index on database level and #UniqueEntity validates on ORM level. So what option shall I use, or do I use all of them?
#UniqueConstraint and unique=true are part of Doctrine and do similar thing.
When you set unique=true on a particular column, then Doctrine will create a unique key on this column physically in database.
#UniqueConstraint can be used to create a unique key in database on multiple columns (complex unique key). But if you pass a single column, then the result will be exactly the same as using unique=true on that field.
#UniqueEntity on the other hand is not a part of Doctrine, but it's a part of Symfony framework. While options above are used by Doctrine to generate proper schema, this one is just a validator used usually by Symfony Form Component at time of submitting the form.
So to answer your final question - yes, you usually should use both #UniqueEntity and one of #UniqueConstraint or unique=true.
As stated in documentation, #UniqueConstraint annotation is used for creation of unique constraint on multiple columns, when unique=true is used for unique constraint on one column.
UniqueEntityValidator exists to show friendly error message and unique database constraint's purpose is to make sure you don't store duplicate data.
So the answer to your question is like this - you should use both database constraint and #UniqueValidator.
Is it possible to update the the Primary Key of a table when using EF Core.
I have tried _context.Database.ExecuteSqlCommand with UPDATE statement as well as using a stored Proc and no joy, db.SaveChanges() gives the following error
System.InvalidOperationException: 'The property 'Id' on entity type 'Simulation' is part of a key and so cannot be modified or marked as modified.'
This is a one off job that needs to be done, I have temporarily modified the foreign keys to ON UPDATE CASCADE.
Is it possible with EF or should I just use ADO?
Thanks
When using code first i have this scenario:
I have an existing database where tables are created with Code first.
Know suddenly there needs to be a change in the program and there is a new table that is related to the Primary table (with existing data in it).
Example there is a table "Package" and a the new table is "PackageState"
Steps
Create new domain Packagestate with some properties.
Add property packagestate (FK) in Package domain.(P) Note that the packagestate is required.
In the seeding class i add some data for the packagestate
From this point i have a problem, because the value is required, and the seeding method is only executed after update.
How do you solve this with code first?
Because it's possible that there is a new migration file as well.
I need some feedback on this thanks in advance!
You must make your foreign key nullable and update your database
after that you can run your seed.
and after all you should change your foreign key to not null.
One of the database view I am trying to import using entity framework contains only two columns, one is an integer type of column and another one is an aggregate function. I am getting the following error.
The table/view does not have a primary key defined and no valid primary key could be inferred. This table/view has been excluded. To use the entity, you will need to review your schema, add the correct keys, and uncomment it.
I understand it is a known scenario and it can be fixed by either including a Key column in the view or modifying the edmx file manually.
I just wanted to know if there is some other solution other than the above two? I do not want to include an additional column in my query and making changes in edmx is not feasible as DB changes are very frequent and the edmx will be overwritten every time I update from db.
You can mark both properties as entity key directly in the designer but you must ensure that the composite value of these two properties will be always unique. If you cannot ensure that you must add another unique column anyway or you may have some other problems when working with such entity set.