How to seed data with AddOrUpdate with a complex key in EF 4.3
Asked Answered
T

2

106

I am trying to seed a development database with some test data.

I have used context.People.AddOrUpdate(p => p.Id, people)); with much success.

I have another table that I need to seed, in which I would not know the primary key.

For example, I would want to AddOrUpdate based on the First and Last names matching.

I am unsure how to write the Expression correctly.

context.People.AddOrUpdate(p => p.FirstName && p.LastName, people);

is obviously incorrect, but I hope it conveys the solution I am looking for.

Trogon answered 19/4, 2012 at 18:57 Comment(0)
I
211

Try this:

context.People.AddOrUpdate(p => new { p.FirstName, p.LastName }, people);
Indemnity answered 19/4, 2012 at 20:51 Comment(7)
@LadislavMrnka what if the identifier needs to be a complex type i.e. context.People.AddOrUpdate(p => new { p.Name.FirstName, p.Name.LastName }, people)?Atrophied
@LadislavMrnka, also, what if the property is a nullable type? i.e. context.People.AddOrUpdate(p => new { p.Birthdate }, people) ?Vetter
Something to note here is that the 'people' collection needs to be an ARRAY and not a list. If you have a list of entities, you can simply call .ToArray() on the list. I struggled with that one :) - Good answerMantellone
can't get this to work. possibly because (in addition to 3 properties specified in composite key) i have a another ID field with auto generated numbers?Allocution
@LadislavMrnka is need to keep Migration folder(Configuration.cs and...) after do migration and update database fields???Ewaewald
@Atrophied You can use the Id column for your complex type. You can do: context.Names.AddOrUpdate(n => new { n.FirstName, n.LastName, }, name); context.People.AddOrUpdate(p => new { p.NameId, }, people);. It will work even if both are newly added to the database assuming the default value C# value of your Name.Id property is not a valid value for that column in the database—i.e., if you use the default generated IDENTITY(1, 1) and not IDENTITY(0, 1) you’ll be fine.Writhe
How to add navigation property of other table foreign key in code first approach? I have structure like context.People.AddOrUpdate(p => new { p.Name.FirstName, p.Name.LastName }, people) ? It this possible?Polik
T
1

If you got Only primitive types or enumeration types are supported in this context. because of using navigation property - consider adding foreign key property directly to the entity (maybe only with getter) and use it as Ladislav Mrnka proposed.

Tachycardia answered 17/2, 2017 at 12:28 Comment(2)
How to add navigation property of other table foreign key in code first approach? I have structure like context.People.AddOrUpdate(p => new { p.Name.FirstName, p.Name.LastName }, people) ? It this possiblePolik
If I set getter property error is The specified type member 'NameId' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.Polik

© 2022 - 2024 — McMap. All rights reserved.