im messing around with code first and Mvc creating a Database.SetIntializer class which overrides the base seed method.
my problem is one of my model properties is a List<t> so when I try to seed with a new type it cannot implicitly converty my type to sys.gen.collection List<type>.
public class CodeFirstDbInitializer : DropCreateDatabaseIfModelChanges<CodeFirstDb>
{
protected override void Seed(CodeFirstDb context)
{
context.Person.Add(new Person
{
FirstName = "James",
LastName = "Grime",
Age = 27,
Address = new Address
{
number = 41,
street = "Faraway close",
Town = "Farishton",
City = "Manchester",
County = "Lancashire",
PostCode = "1i3s",
}
});
base.Seed(context);
}
}
I've tried writing this a few ways such as
Address.Add( new person {x, y, z})
but nothing any ideas, cheers.
public class Person
{
private int _id;
public int ID { get { return _id; } }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public List<Address> Address { get; set; }
}
public class Address
{
private int _id;
public int ID { get { return _id; } }
public int number { get; set; }
public string street { get; set; }
public string Town { get; set; }
public string City { get; set; }
public string County { get; set; }
public string PostCode { get; set; }
public List<Person> Tenants { get; set; }
}
I just wanted to see how it handles transact tables etc so these are just quick and dirty classes. I have changed them since to try testing when these properties are not lists
P.s. how do I step out of code mode on here once ive pasted code in to return to normal comment text?
Okay, so my assumptions about your returned type was a bit off.
I see that you have a List<Address> in your Person class. I'm not sure that there is any reasonable way for MVC to map to that type.
As for your P.s. question, the way to get out of code mode is to open a few more new lines before pasting code so that you have non-code lines to continue typing in after the code.
Ok figured this out a little after but didn't think to replay.
when overriding and seeding a database with MVC/EF codefirst... well anywhere you employ anonymous type initialisation on System.Generic.Collections you need to initialise a new list also.
Address = new List<Address> {
new Address { ... }, new Address { ... } ...
}
So its important to initialise the collection because the class only specifies the type of the property and does not initialise it, I'm a noob.
Thanks for your help all and hope this helps.
JTGrime
Marked as answer by jtgrime on Feb 21, 2012 08:53 PM
JTGrime
Member
41 Points
27 Posts
seeding a simple model
Feb 18, 2012 07:04 PM|LINK
Hi all,
im messing around with code first and Mvc creating a Database.SetIntializer class which overrides the base seed method.
my problem is one of my model properties is a List<t> so when I try to seed with a new type it cannot implicitly converty my type to sys.gen.collection List<type>.
public class CodeFirstDbInitializer : DropCreateDatabaseIfModelChanges<CodeFirstDb> { protected override void Seed(CodeFirstDb context) { context.Person.Add(new Person { FirstName = "James", LastName = "Grime", Age = 27, Address = new Address { number = 41, street = "Faraway close", Town = "Farishton", City = "Manchester", County = "Lancashire", PostCode = "1i3s", } }); base.Seed(context); } } I've tried writing this a few ways such as Address.Add( new person {x, y, z}) but nothing any ideas, cheers.eric2820
Contributor
2777 Points
1161 Posts
Re: seeding a simple model
Feb 18, 2012 07:31 PM|LINK
Posting your base class might shed some light on this problem and allow more useful comentary.
http://www.my-msi.net/Admin
blog
If a post helps you, please mark it as Ansered, thank-you.
JTGrime
Member
41 Points
27 Posts
Re: seeding a simple model
Feb 18, 2012 07:36 PM|LINK
Sorry about that
public class Person { private int _id; public int ID { get { return _id; } } public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } public List<Address> Address { get; set; } } public class Address { private int _id; public int ID { get { return _id; } } public int number { get; set; } public string street { get; set; } public string Town { get; set; } public string City { get; set; } public string County { get; set; } public string PostCode { get; set; } public List<Person> Tenants { get; set; } } I just wanted to see how it handles transact tables etc so these are just quick and dirty classes. I have changed them since to try testing when these properties are not lists P.s. how do I step out of code mode on here once ive pasted code in to return to normal comment text?eric2820
Contributor
2777 Points
1161 Posts
Re: seeding a simple model
Feb 18, 2012 07:48 PM|LINK
Okay, so my assumptions about your returned type was a bit off.
I see that you have a List<Address> in your Person class. I'm not sure that there is any reasonable way for MVC to map to that type.
As for your P.s. question, the way to get out of code mode is to open a few more new lines before pasting code so that you have non-code lines to continue typing in after the code.
http://www.my-msi.net/Admin
blog
If a post helps you, please mark it as Ansered, thank-you.
JTGrime
Member
41 Points
27 Posts
Re: seeding a simple model
Feb 18, 2012 07:54 PM|LINK
thank you so much for your time its much appreciated.
JTGrime
Member
41 Points
27 Posts
Re: seeding a simple model
Feb 21, 2012 08:53 PM|LINK
Ok figured this out a little after but didn't think to replay.
when overriding and seeding a database with MVC/EF codefirst... well anywhere you employ anonymous type initialisation on System.Generic.Collections you need to initialise a new list also.
Address = new List<Address> { new Address { ... }, new Address { ... } ... }So its important to initialise the collection because the class only specifies the type of the property and does not initialise it, I'm a noob.
Thanks for your help all and hope this helps.