Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entering keys manually with Entity Framework

I'm trying to use Entity Framework code first for a simple database project and I run into a problem I simply cannot figure out.

I noticed EF was setting the ID for my tables automatically increasing by 1 each time, completely ignoring the value I entered manually for that field. After some searching it is my understanding that the right way to disable this behavior is doing:

modelBuilder.Entity<Event>().Property(e => e.EventID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); 

However now I'm just getting this error and I have no idea why:

Unhandled Exception: System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---

System.Data.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Cannot insert explicit value for identity column in table 'Events' when IDENTITY_INSERT is set to OFF.

If it's helpful, here is the POCO class in question:

public class Event {     [Key, Required]     public int EventID { get; set; }      public string EventType { get; set; } //TODO: Event Type Table later on     public DateTime StartDate { get; set; }     public DateTime EndDate { get; set; }      public virtual ICollection<Match> Matches { get; set; }     public virtual ICollection<EventParticipation> EventParticipation { get; set; } } 

Thanks in advance.

like image 260
JCafe Avatar asked Sep 20 '13 01:09

JCafe


People also ask

How do I set primary key in Entity Framework?

Configuring a primary key By convention, a property named Id or <type name>Id will be configured as the primary key of an entity. Owned entity types use different rules to define keys. You can configure a single property to be the primary key of an entity as follows: Data Annotations.

Do we use foreign keys in Entity Framework?

The ForeignKey attribute is used to configure a foreign key in the relationship between two entities in EF 6 and EF Core. It overrides the default conventions. As per the default convention, EF makes a property as foreign key property when its name matches with the primary key property of a related entity.

Can Entity Framework work without primary key?

Because Entity Framework is able to modify data in your database it needs a primary key to be defined in order for it to be able to uniquely identify rows.

How do I create a composite key in Entity Framework Core?

The only way to configure composite keys is to use the HasKey method. You specify the properties that form the composite key by passing them in as properties of an anonymous type to the HasKey method.


1 Answers

Since I prefer attributes, here the alternative for the sake of completeness:

using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema;  [Key] [DatabaseGenerated(DatabaseGeneratedOption.None)] public int Id { get; set; } 

Note: This works also in EF Core.

like image 127
marsze Avatar answered Sep 22 '22 04:09

marsze