Problem with Inserting new row with EF in 1...0,1 Relationship

entity framework add new record with existing foreign key
entity framework update one to many relationship
entity framework save object with foreign key\
entity framework update related entities
entity framework many-to-many insert
ef core add entity with foreign key
entity framework create entity with relationships
ef core entitystate detached

I have two tables, Person and Employee and i have newly created 1 .. 0,1 relationship between them respectively. PersonId in Person table had defined identity 1. EmployeeId in Employee table doesnt have identity defined.

When i insert new row into Person table with SQL Studio everything works fine, however when i try to insert data with Entity Framework i get error:

Cannot insert explicit value for identity column in table 'Person' when IDENTITY_INSERT is set to OFF.

PersonId is not explicitly defined and is 0 in time of saving.

Note that inserting new person row is working fine without relationship with employee.

Am I missing something obvious or my approach is completly wrong? Googling sadly didnt helped at all.

EDIT: I have found solution, but i will add entity code for people facing same problem.

Person entity:

public partial class Person
{
    public Person()
    {
    }

    [Key]
    public int PersonId { get; set; }
    ...
    public virtual Employee Employee { get; set; }

Employee Entity:

public partial class Employee
{
    public Employee()
    {
    }

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int EmployeeId { get; set; }
    ...
    public Data.Dal.Person Person { get; set; }

Relationship defined in context:

modelBuilder.Entity<Employee>()
   .HasRequired(s => s.Person)
   .WithRequiredPrincipal(ad => ad.Employee);

Do you you auto increment on your Id column ? Defined in your SQL database. If it is the case then try to decorate the corresponding property of your entity with DatabaseGenerated.Identity attribute.

Issue when inserting new entity with relationships to existing entities , I modified some code, please review it. In class DbRepository , added another constructor, to make sure there is the same DbContext in  When i insert new row into Person table with SQL Studio everything works fine, however when i try to insert data with Entity Framework i get error: Cannot insert explicit value for identity column in table 'Person' when IDENTITY_INSERT is set to OFF. PersonId is not explicitly defined and is 0 in time of saving.

I found the solution. I am using Code First approach for generating Data Object Model. EF created this relationship as:

    modelBuilder.Entity<Employee>()
        .HasRequired(s => s.Person)
        .WithRequiredPrincipal(ad => ad.Employee);

By EF definition Employee is main refereced class, which is wrong. I changed referenced class to Person and everything works fine so far.

Correct relationship:

    modelBuilder.Entity<Employee>()
        .HasRequired(s => s.Person)
        .WithRequiredDependent(ad => ad.Employee);

More about having referece and being reference in answer: Ef code first diference between principle and dependent

Entity Framework 4.0 Recipes: A Problem-Solution Approach, A Problem-Solution Approach Larry Tenny, Zeeshan Hirani Because of the 1:0.​.1 relationship, we cannot have a row in the Retail or eCommerce tables Listing 2-9 demonstrates inserting and retrieving from our model. using (var context = new EFRecipesEntities()) { var business = new Business { Name = "​Corner Dry  Dealing with 1:0..1 Relationships in Entity Framework 4 January 20, 2011 Cedric Bertolasio Leave a comment Go to comments In this post I attempt to illustrate how I have used Entity Framework 4 to deal with inserting, fetching, and deleting a database record for the 0..1 side of a 1:0..1 relationship.

I think you can solve your problem with SQL Server,make relationship with employee in your SQL server and try inserting new person again.

Mechanics of Fluids, Line L (m) D (mm) e (mm) SK 1 200 100 0.1 2 2 150 50 0.1 30 3 500 100 0.1 0 4 35 el 116 m el 118 m E F [5] D [3] [4] B C [1] [2] Figure P11.36 A 11.37 In Problem 11.36, if a pump supplying 10 kW of useful power was inserted near A, and the pump curve is approximated by the relation H a aQ aQ aQ P 5 1 1 1 0 1 22 33. And again, the EntityState of the Topic instance creates confusion: EF will insert a new Topic and use the value for that new row’s Id when it inserts the Screencast. The safest way to avoid this is to set the Topic property to null when you set the foreign key value.

Relationships, navigation properties, and foreign keys, Relationships in EF; Configuring or mapping relationships; Creating and modifying Note, that when working with 1-to-1 or 1-to-0..1 relationships, there is no By assigning a new value to a foreign key property, as in the By deleting or adding an object in an entity collection. There are no open issues. [ Entity Framework ] Insert new record with relationship one-to-many [Answered] RSS 3 replies Last post Sep 16, 2013 08:58 AM by dhaval-950

The Educational Times and Journal of the College of Preceptors, #-2bc; b + c o + c therefore, a homogeneous relation between the invariants, Hence the line joining the points of contact of common tangents touches the This gives a solution of Mr. Wolstebholhb'S Question 2744. Join X)E, EF, PA, PB. row by adding up 3 times successively the progression 1, 0, 0, 0, 0, 0, 3rd row  This all works well with npgsql except when inserting a new row. The RETURNING statement that is generated would be as follows: insert into myschema.mytable etc returning colA,colB,status. which fails. So I downloaded the library and got it to work by passing the table name and schema name into AppendReturningClause. And inserts now work for me.

Solving the Detached Many-to-Many Problem with the Entity , The problem with detached entities is that the data context has no way of Add a relationship (many-to-many); Add a relationship (FK-based); Update a Migrations namespace and it acts as an 'Upsert' operation, inserting or save-an​-updated-many-to-many-collection-on-detached-entity-framework-4-1  Here Mudassar Ahmed Khan has explained with an example, how to insert (save) multiple rows (records) to database using Entity Framework in ASP.Net MVC Razor. First the multiple rows (records) will be insert into an HTML Table and then the data from all the rows of the HTML Table will be sent to Controller using jQuery AJAX which will be ultimately saved to database using Entity Framework in

Comments
  • Can you put your entities as well? I mean how your entity look like in C#?