none
EF 4.1 Database First - Inserting Against a One-to-One Relationship RRS feed

  • Question

  • Hello,

    I've ran into an issue using Entity Framework 4.1. I'm working on a ASP MVC3 project that the database is done first by a DBA team. A one-to-one relationship was setup between the Applicants table and a TestResults table. The TestResults table's Primary Key is also a Foreign Key for the Applicant.

    In the app, an Applicant is created before moving onto test results. The problem I'm running into is inserting TestResults. Setting the ApplicantID key in the TestResults table is telling Entity Framework that this should be an update rather than an insert (or at least that's what I'm assuming because the error is: Store update, insert, or delete statement affected an unexpected number of rows (0)). Which tells me its a concurrecny issue. Any updates done on existing test results work fine, it's just the insert that's failing.

    Does anyone have any input on make EF realize that I'm trying to insert with a Primary/Foriegn Key than updating an existing record?

    Thanks.

    Tuesday, March 27, 2012 10:13 PM

Answers

  • Hi jchmura,

    Welcome!

    I'll show you how to insert against a one-to-one relationship:

     using (var db= new EFTestEntities())
                {
                    // insert with new Applicants
                    var a1 = new Applicants { Id = 3, Name = "A3" };
                    var t1 = new TestResults { Applicants = a1 };
                    db.TestResults.Add(t1);
                    db.SaveChanges();
                    // 
                    var a2 = new Applicants { Id=4, Name="A4"};
                    db.Applicants.Add(a2);
                    var t2 = new TestResults { Id = 4 };
                    db.TestResults.Add(t2);
                    db.SaveChanges();
                }
    //My database
    create table Applicants
    (
    Id int primary key,
    Name nvarchar(20) not null
    )
    create table TestResults
    (
    Id int primary key,
    Test nvarchar(20)
    )
    alter table TestResults add constraint FK_App_Test foreign key(Id) references Applicants(Id)
    
    
    select * from Applicants
    select * from TestResults

    Have a nice day.

    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by jchmura-az Wednesday, March 28, 2012 2:06 PM
    Wednesday, March 28, 2012 1:57 AM
    Moderator

All replies

  • Hi jchmura,

    Welcome!

    I'll show you how to insert against a one-to-one relationship:

     using (var db= new EFTestEntities())
                {
                    // insert with new Applicants
                    var a1 = new Applicants { Id = 3, Name = "A3" };
                    var t1 = new TestResults { Applicants = a1 };
                    db.TestResults.Add(t1);
                    db.SaveChanges();
                    // 
                    var a2 = new Applicants { Id=4, Name="A4"};
                    db.Applicants.Add(a2);
                    var t2 = new TestResults { Id = 4 };
                    db.TestResults.Add(t2);
                    db.SaveChanges();
                }
    //My database
    create table Applicants
    (
    Id int primary key,
    Name nvarchar(20) not null
    )
    create table TestResults
    (
    Id int primary key,
    Test nvarchar(20)
    )
    alter table TestResults add constraint FK_App_Test foreign key(Id) references Applicants(Id)
    
    
    select * from Applicants
    select * from TestResults

    Have a nice day.

    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by jchmura-az Wednesday, March 28, 2012 2:06 PM
    Wednesday, March 28, 2012 1:57 AM
    Moderator
  • Alan_chen,

    Thanks for the response! What you provided worked. For inserting though I changed it so I would add the test result to the applicant and save the applicant. That seemed to solve the insert problem. Doing an update works with what you have above.

    Wednesday, March 28, 2012 2:07 PM