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

后端 未结 15 1303
难免孤独
难免孤独 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:57

    Just for others who might have similar problems. I had the same error, but for a different reason. In one of the child objects I defined the [Key] as being a value which was the same for different saves. A stupid mistake on my part, but the error message does not instantly lead you to the problem.

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

    This problem can also arise from reversed key declarations. If you're using fluent to configure the relationship, make sure the left and right keys are mapped to the correct entity.

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

    Yet another different case here. A query was cast to a list and while doing that, it created entities by their constructor for comparison in the linq expression right after the ToList(). This created entities that gotten into the deleted state after the linq expression finished.
    However! There was a small adjustment that created another entity in the constructor, so that this new entity got linked to an entity that was marked as Deleted.

    Some code to illustrate:

    query.Except(_context.MyEntitySetSet()
                    .Include(b => b.SomeEntity)
                    .Where(p => Condition)
                    .ToList() // This right here calls the constructor for the remaining entities after the where
                    .Where(p => p.Collection.First(b => Condition).Value == 0)
                    .ToList();
    

    The constructor of MyEntity:

    public partial class MyEntity
    {
        protected MyEntity()
        {
            // This makes the entities connected though, this instance of MyEntity will be deleted afterwards, the instance of MyEntityResult will not.
            MyEntityResult = new MyEntityResult(this);
        }
    }
    

    My solution was to make sure the entire expression was done inside the IQueryable so that there won't be any objects created.

    0 讨论(0)
  • 2020-12-08 03:01

    After a bit of investigation I found that whilst .Net supports a minimum date (DateTime.MinValue) of 01/01/0001 00:00:00 and a maximum (DateTime.MaxValue) of 31/12/9999 23:59:59 in SQL Server Compact Edition minimum date is 01/01/1753 00:00:00. When I entered a date greater than 01/01/1753 00:00:00, this error disappeared.

    0 讨论(0)
  • 2020-12-08 03:03

    I had the same error and in my case the problem was that I added a relationship object which had already been loaded "AsNoTracking". I had to reload the relation property.

    BTW, Some suggest using "Attach" for relations that already exist in db, I haven't tried that option though.

    0 讨论(0)
  • 2020-12-08 03:03

    I'm not entirely sure that it's going to help in your case because I'm setting up my tables using Fluent API, however, as far I can tell, the issue arises regardless whether the schema is set up using data annotations (attributes) or Fluent API (configuration).

    There seems to be a bug in EF (v. 6.1.3) as it omits certain changes to the schema when updating the DB to the next migration. The quickest route around it is (during the development stage) to remove all the tables from the DB and runt migrations from init stage again.

    If you're already in production, the quickest solution I've found was to manually change the schema in the DB or, if you want to have version control of the changes, manually manipulate the methods Up() and Down() in your migration.

    0 讨论(0)
提交回复
热议问题