An error occurred while saving entities that do not expose foreign key properties for their relationships

后端 未结 15 1301
难免孤独
难免孤独 2020-12-08 02:43

I have a simply code in Entity Framework 4.1 code first:

PasmISOContext db = new PasmISOContext();
var user = new User();
user.CreationDate = Da         


        
相关标签:
15条回答
  • 2020-12-08 02:44

    I hade same probleme. in my case, it was due to datetime field with a null value. I had to passe a value to datetime and evrythings went fine

    0 讨论(0)
  • 2020-12-08 02:45

    The issue was resolved by adding an FK property.

    0 讨论(0)
  • 2020-12-08 02:45

    In my case the exeception was thrown because EF had created a migration incorrectly. It missed setting the identity: true on the second table. So go into the migrations which created the relevant tables and check if it missed to add identity.

    CreateTable(
        "dbo.LogEmailAddressStats",
        c => new
            {
                Id = c.Int(nullable: false, identity: true),
                EmailAddress = c.String(),
            })
        .PrimaryKey(t => t.Id);
    
    CreateTable(
        "dbo.LogEmailAddressStatsFails",
        c => new
            {
                Id = c.Int(nullable: false), // EF missed to set identity: true!!
                Timestamp = c.DateTime(nullable: false),
            })
        .PrimaryKey(t => t.Id)
        .ForeignKey("dbo.LogEmailAddressStats", t => t.Id)
        .Index(t => t.Id);
    

    An Id column should have identity (i.e. auto-incrementing!) so this must be a EF bug.

    You could add identity manually with SQL directly to the database but I prefer using Entity Framework.

    If you run in to the same problem I see two easy solutions:

    Alt 1

    reverse the incorrectly created migration with

    update-database -target:{insert the name of the previous migration}
    

    Then add the identity: true manually to the migration code and then update-database again.

    Alt 2

    you create a new migration that adds identity. If you have no changes in the models and you run

    add-migration identity_fix
    

    it will create an empty migration. Then just add this

        public partial class identity_fix : DbMigration
        {
            public override void Up()
            {
                AlterColumn("dbo.LogEmailAddressStatsFails", "Id", c => c.Int(nullable: false, identity: true));
            }
    
            public override void Down()
            {
                AlterColumn("dbo.LogEmailAddressStatsFails", "Id", c => c.Int(nullable: false));
            }
        }
    
    0 讨论(0)
  • 2020-12-08 02:46

    Today I faced this issue and tried the possible solutions posted above but none of them helped me. I had UnitOfWork pattern implemented and system was committing the data in last after adding all the records.

    In my case system was combining the two models and querying the DB

    Invalid object name 'dbo.RoleModelUserModel'.

    where these were two different models actually.

    I fixed this by reordering the insert statements and adding the parent entity first. In this case added the user first and issue resolved.

    0 讨论(0)
  • 2020-12-08 02:51

    In my case, the problem was that I renamed a column improperly, so the migration made two columns, one called "TeamId" and one called "TeamID". C# cares, SQL doesn't.

    0 讨论(0)
  • 2020-12-08 02:53

    In my case the following situation was giving me the same Exception:

    Imagine a code first EF model where you have a Garage entity that has a collection of Car entities. I needed to remove a car from the garage so I ended up with code that looked like this:

    garageEntity.Cars.Remove(carEntity);
    

    Instead, it should've been looked like this:

    context.Cars.Remove(carEntity);
    
    0 讨论(0)
提交回复
热议问题