Address newAddress = Address.CreateAddress(0, "HOME", "101 MAIN ST",
"BALTIMORE", "MD", 21202);
// attach the contact to the address
newAddress.Contact = newContact;
ctx.AddToAddresses(newAddress);
// save the contact and the address
ctx.SaveChanges();
// query all contacts
var queryResults = from c in ctx.Contacts
select c;
foreach (Contact c in queryResults)
{
Console.WriteLine("{0} {1} {2}", c.ContactID,
c.FirstName, c.LastName);
}
}
But my problem is that my code can not find the CreateContact and AddToContacts method. The only difference of my Model is that mines does not have the one to many relationship, and does not set up Naviation property, the rest of fields are same. And "Code
Generation Strategy" set to "None", (I got some errors such entity already contains member or dupliacte fields if I set it to "Default", so I make it "None")
When I entered Contact. the intellingence doesnot prompt me the CreateContact method, and I got error I added myself.
Anybody knows what is wrong with my entity? Thanks
An EntityFramework doesn't have such a method. You have to create another new instance and do add.
Decker Dong - MSFT
Peter Cong
context.CreateCommitments(newCom);
Hi,
An EntityFramework doesn't have such a method. You have to create another new instance and do add.
Hi Decker, Thanks a lot for your help,
I just start learning the entityframework.
You are saying I have to create such class, and entityframework does not have such method.
But I am using Database first to generate the Entity (NOT code first), the SQL tables are ready created in SQL server (or my local ) which can be navigated by SQL management studio. please correct me if I am wrong: I think the database first way to create
entity, all classes related to the entity are already generated automatically, I do not need worry about it and create extra by myself, is that right?
I am using VS2012, in my solution explore, under Model1.edmx, I can see Model1.Context.tt, Model1.Designer.cs, Model1.edmx.diagram and Model1.tt are all generated. based on the tutorials, I think the Model1.Designer.cs contains all class files related to
my entity, right?
my project can be compiled wthout any errors, but when I change the Code Generatin Strategy( under the property of .edmx) from "None" to "Default", I got a lot of compile error in error list which comes from Model1.Designer.cs file. the error is something
like the entity has alraedy existed or memember already existed.
Please let me know what is wrong with my entity creation process, and what steps are missing which caused me can not save data to table.
But I am using Database first to generate the Entity (NOT code first),
Yes, I see that you are using Db-first. But as far as I see, it doesn't have such a method, either.
Peter Cong
my project can be compiled wthout any errors, but when I change the Code Generatin Strategy( under the property of .edmx) from "None" to "Default", I got a lot of compile error in error list which comes from Model1.Designer.cs file. the error is something like
the entity has alraedy existed or memember already existed.
But I am using Database first to generate the Entity (NOT code first),
Yes, I see that you are using Db-first. But as far as I see, it doesn't have such a method, either.
Peter Cong
my project can be compiled wthout any errors, but when I change the Code Generatin Strategy( under the property of .edmx) from "None" to "Default", I got a lot of compile error in error list which comes from Model1.Designer.cs file. the error is something like
the entity has alraedy existed or memember already existed.
Have you done anything else to change the code?
Nothing changes, everything is default. but do not know why when I select Code Generatin Strategy = "Defult", a lot of compiled time error coming up in the .design.cs error list.
Peter Cong
Member
527 Points
681 Posts
Can not find the CreateXXXX or AddXXXX method in my xxx.designer.cs file, help please.
Dec 14, 2012 04:01 AM|LINK
Hi, I follows this simple example to create entity data model:
http://www.mssqltips.com/sqlservertip/1963/intro-to-entity-framework-with-sql-server/
I tried these codes:
using (EFSampleEntities ctx = new EFSampleEntities())
{
// create a new contact
Contact newContact = Contact.CreateContact(0, "JONES", "SAM");
ctx.AddToContacts(newContact);
// create an address for the contact
Address newAddress = Address.CreateAddress(0, "HOME", "101 MAIN ST",
"BALTIMORE", "MD", 21202);
// attach the contact to the address
newAddress.Contact = newContact;
ctx.AddToAddresses(newAddress);
// save the contact and the address
ctx.SaveChanges();
// query all contacts
var queryResults = from c in ctx.Contacts
select c;
foreach (Contact c in queryResults)
{
Console.WriteLine("{0} {1} {2}", c.ContactID,
c.FirstName, c.LastName);
}
}
But my problem is that my code can not find the CreateContact and AddToContacts method. The only difference of my Model is that mines does not have the one to many relationship, and does not set up Naviation property, the rest of fields are same. And "Code Generation Strategy" set to "None", (I got some errors such entity already contains member or dupliacte fields if I set it to "Default", so I make it "None")
When I entered Contact. the intellingence doesnot prompt me the CreateContact method, and I got error I added myself.
Anybody knows what is wrong with my entity? Thanks
RameshRajend...
Star
7983 Points
2099 Posts
Re: Can not find the CreateXXXX or AddXXXX method in my xxx.designer.cs file, help please.
Dec 14, 2012 04:05 AM|LINK
Here is my simple sample code.You can check here
var modelUser = datamodel.Users.FirstOrDefault(u => u.UserName == HttpContext.Current.User.Identity.Name); JobPosting posting = new JobPosting(); posting.PostDate = DateTime.Now; posting.Company = new Company { Name = jobPosting.Company }; posting.Desciption = jobPosting.Description; posting.Title = jobPosting.Title; posting.SalaryTo = jobPosting.SalaryTo; posting.SalaryFrom = jobPosting.SalaryFrom; posting.SalaryTermId = jobPosting.JobPostingSalaryTerm; posting.IsDeleted = false; var currentAddress = bingRepository.ToDatabaseAddress(jobPosting.Address, LocationTypes.Job, out errorString); currentAddress.UserInfo = modelUser.UserInfo; posting.Addresses.Add(currentAddress); modelUser.JobPostings.Add(posting); datamodel.SubmitChanges();Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Can not find the CreateXXXX or AddXXXX method in my xxx.designer.cs file, help please.
Dec 15, 2012 12:40 AM|LINK
What's the class of that? Is "CreateContact" a static class that returns you a type of itself?
What's your entity model?
Peter Cong
Member
527 Points
681 Posts
Re: Can not find the CreateXXXX or AddXXXX method in my xxx.designer.cs file, help please.
Dec 15, 2012 01:53 AM|LINK
Thanks for the quick response.
This is my case:
I have a entity data model created: this is connectionString in my web.config file.
<connectionStrings> <add name="ITECEntitiesT1" connectionString="metadata=res://*/ITModel1.csdl|res://*/ITModel1.ssdl|res://*/ITModel1.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\ITEC.mdf;integrated security=True;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /> </connectionStrings>
Basically I create a button on my web form to try to add data to my table, very simple. this is the code:
protected void Button1_Click(object sender, EventArgs e) {
using (ITECEntitiesT1 context = new ITECEntitiesT1())
{
//create a new commitment(try to insert data to my table: Commitments
Commitment newCom = new Commitment();
newCom.CommitNo = "aaaa";
newCom.CommitDesc ="bbb";
context.CreateCommitments(newCom); //here the compiler does not recognize the CreateCommitments(newCom), I also tried Add(newCom), not complie as well
context.SaveChanges(); } }
Basically, I do not know how to use the methods in my design.cs class in order to insert the data to my SQL table.
How can I use it correctly? thanks
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Can not find the CreateXXXX or AddXXXX method in my xxx.designer.cs file, help please.
Dec 15, 2012 05:54 AM|LINK
Hi,
An EntityFramework doesn't have such a method. You have to create another new instance and do add.
Peter Cong
Member
527 Points
681 Posts
Re: Can not find the CreateXXXX or AddXXXX method in my xxx.designer.cs file, help please.
Dec 15, 2012 01:28 PM|LINK
Hi Decker, Thanks a lot for your help,
I just start learning the entityframework.
You are saying I have to create such class, and entityframework does not have such method.
But I am using Database first to generate the Entity (NOT code first), the SQL tables are ready created in SQL server (or my local ) which can be navigated by SQL management studio. please correct me if I am wrong: I think the database first way to create entity, all classes related to the entity are already generated automatically, I do not need worry about it and create extra by myself, is that right?
I am using VS2012, in my solution explore, under Model1.edmx, I can see Model1.Context.tt, Model1.Designer.cs, Model1.edmx.diagram and Model1.tt are all generated. based on the tutorials, I think the Model1.Designer.cs contains all class files related to my entity, right?
Please let me know what is wrong with my entity creation process, and what steps are missing which caused me can not save data to table.
Thanks
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Can not find the CreateXXXX or AddXXXX method in my xxx.designer.cs file, help please.
Dec 16, 2012 12:29 AM|LINK
Yes, I see that you are using Db-first. But as far as I see, it doesn't have such a method, either.
Have you done anything else to change the code?
Peter Cong
Member
527 Points
681 Posts
Re: Can not find the CreateXXXX or AddXXXX method in my xxx.designer.cs file, help please.
Dec 17, 2012 03:12 PM|LINK
Nothing changes, everything is default. but do not know why when I select Code Generatin Strategy = "Defult", a lot of compiled time error coming up in the .design.cs error list.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Can not find the CreateXXXX or AddXXXX method in my xxx.designer.cs file, help please.
Dec 18, 2012 12:27 AM|LINK
Hi,
I cannot reproduce your issue……And what's more,I think you should cancel setting the property at present.