Come controllare i valori delle chiavi primarie durante la semina i dati con il codice quadro di entità prima

StackOverflow https://stackoverflow.com//questions/12704519

Domanda

Sto creando un sito MVC4 ASP.NET utilizzando Entity Framework 5 con Codefirst e SQL Server Express 2012.

Ho abilitato le migrazioni e ora lo faccio nel mio metodo Configuration.seed: (Nota che voglio impostare la chiave primaria su 8 anche se questo è il primo record nel database).

context.ProductCategoryDtoes.AddOrUpdate(x => x.Id,
    new ProductCategoryDto() { Id = 8, Name = "category1" }
);
.

Il mio oggetto modello è definito come questo:

[Table("ProductCategory")]
public class ProductCategoryDto {
    public long Id { get; set; }
    public string Name { get; set; }
}
.

Questo risulta in una tabella in (SQL Server Express 2012) in cui la colonna ID ha identità= true, semi d'identità= 1, incremento identità= 1.

Ora quando eseguo le migrazioni eseguendo un PM> Update-Database questo risultato in una riga con ID= 1.

Quindi la mia domanda è:

1) Come posso controllare i valori dei tasti primari incrementati automatici quando si secano i dati.

2) Se la soluzione è di incrementare il valore del seme delle colonne chiave, quindi come viene fatto questo quando sto usando Database.SetInitializer(new DropCreateDatabaseAlways<MyContext>());. Questo sarà nulle e ricostruirà il database ogni volta che aggiornerò il database, quindi come verrà aggiornato il valore del seme nel database fresco?

È stato utile?

Soluzione

Basta creare entità fittizie con valori predefiniti, quindi aggiungere i tuoi dati reali e in seguito eliminare i manichini.Non il modo migliore, ma immagino che non ci sia altro ...

Altri suggerimenti

Hai provato ad aggiungerlo in cima alla tua proprietà ID:

[DatabaseGenerated(DatabaseGeneratedOption.None)]
public long Id { get; set; }
.

It seems you are trying to defeat the purpose of an identity column. If you want to do this your only choice is to use SQL Commands Set IDENTITY_INSERT to allow you to insert the value and then run DBCC CHECKIDENT to update the seed. Not a really good idea. These options have security and performance limitations.

You may want to consider using a GUID instead. You can create GUIDs in code which are guaranteed to be unique, and you can also generate GUIDs in SQL as a column default.

With GUIDs, which are non sequential you will need to think through a good indexing strategy. This approach is also debatable.

Ultimately, it looks like you need a different strategy other than using an Identity Column.

It is very hackish, but I ran into a scenario where I had to do it due to some report having hard-coded PK values. Fixing the reports was beyond my scope of work.

Context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT dbo.ProductCategoryDto ON " +
     "INSERT INTO dbo.ProductCategoryDto (Id, Name) VALUES (8, 'category1') " +
     "SET IDENTITY_INSERT dbo.ProductCategoryDto OFF");
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top