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
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.
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.
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
SELECT DISTINCT TOP (100)
PERCENT country_Code, country_Name
FROM dbo.Location
Message 1 The table/view 'mixtapez.dbo.View_Select_Country' does not
have a primary key defined. The key has been inferred and the
definition was created as a read-only
table/view. E:\1C#asp.net\vuziq\vuziq\Projects\BannerSystem\WebBannerSystem\WebBannerSystem\Models\Model1.edmx 0 0 WebBannerSystem
The view works on others languages, so I dont want get Id by distinct, any idea?
It is just an informative message that Entity Framework created what it thinks is the primary key because any Entity Framework entity must have a primary key (that is .NET key, not SQL key). If you have read-only entity, the autogenerated key will probably work just fine for you. To be extra safe (and remove any chance the key does not work correctly), you should use NoTracking option for queries on this entity.
If you use .Distinct() in LINQ query that will go into your SQL query - the key Entity Framework uses does not play any role.
I am unable to understand the line in bold from this msdn page:-
http://msdn.microsoft.com/en-us/library/bb738618.aspx
SaveChanges can generate an UpdateException when an object added to the ObjectContext cannot be successfully created in the data source. This can happen if a row with the foreign key specified by the relationship already exists. When this occurs, you cannot use Refresh to update the added object in the object context. Instead, reload the object with a value of OverwriteChanges for MergeOption.
In a table, a foreign key column can have a single value multiple times. e.g. DepartmentID foreign key in Users Table: More than one User can have same DepartmentID foreign key.
So how can this cause an UpdateException ?
Easy. You have an entity in your context in the Added state with a PK value of something already in the DB. This is common when people try to use stub objects incorrectly. If you want more help than that, you need to isolate your problem and post your code.