c# - Entity Framework Code First, foreign key/object should not save -


i have 3 classes, so:

[datacontract] public class applicationdto : businessbase<int> {     /// <summary>     /// gets or sets name.     /// </summary>     /// <value>the name.</value>     [datamember]     public string name { get; set; }      /// <summary>     /// gets or sets description.     /// </summary>     /// <value>the description.</value>     [datamember]     public string description { get; set; }      /// <summary>     /// gets or sets development startdate.     /// </summary>     /// <value>the development startdate.</value>     [datamember]     public datetime developmentstartdate { get; set; }      /// <summary>     /// gets or sets launch date.     /// </summary>     /// <value>the launch date.</value>     [datamember]     public datetime launchdate { get; set; } }  [datacontract] public class customerdto : businessbase<int> {     /// <summary>     /// gets or sets name of user.     /// </summary>     /// <value>the name of user.</value>     [datamember()]     public string username { get; set; }      /// <summary>     /// gets or sets first name.     /// </summary>     /// <value>the first name.</value>     [datamember()]     public string firstname { get; set; }      /// <summary>     /// gets or sets last name.     /// </summary>     /// <value>the last name.</value>     [datamember()]     public string lastname { get; set; }      /// <summary>     /// gets or sets name of company.     /// </summary>     /// <value>the name of company.</value>     [datamember()]     public string companyname { get; set; }      /// <summary>     /// gets or sets phone.     /// </summary>     /// <value>the phone.</value>     [datamember()]     public string phone { get; set; }      /// <summary>     /// gets or sets email.     /// </summary>     /// <value>the email.</value>     [datamember()]     public string email { get; set; }      /// <summary>     /// gets or sets address1.     /// </summary>     /// <value>the address1.</value>     [datamember()]     public string address1 { get; set; }      /// <summary>     /// gets or sets address2.     /// </summary>     /// <value>the address2.</value>     [datamember()]     public string address2 { get; set; }      /// <summary>     /// gets or sets city.     /// </summary>     /// <value>the city name.</value>     [datamember()]     public string city { get; set; }      /// <summary>     /// gets or sets state region.     /// </summary>     /// <value>the state region.</value>     [datamember()]     public string stateregion { get; set; }      /// <summary>     /// gets or sets zip code.     /// </summary>     /// <value>the zip code.</value>     [datamember()]     public string zipcode { get; set; }      /// <summary>     /// gets or sets country id.     /// </summary>     /// <value>the country id.</value>     [datamember()]     public int countryid { get; set; }      /// <summary>     /// gets or sets ean number.     /// </summary>     /// <value>the ean number.</value>     [datamember()]     public string eannumber { get; set; }      /// <summary>     /// gets or sets vat number.     /// </summary>     /// <value>the vat number.</value>     [datamember()]     public string vatnumber { get; set; }      /// <summary>     /// gets or sets time zone id.     /// </summary>     /// <value>the time zone id.</value>     [datamember()]     public string timezoneid { get; set; } }  [datacontract] public class applicationinstancedto : businessbase<int> {     /// <summary>     /// gets or sets customer id.     /// </summary>     /// <value>the customer id.</value>     [datamember]     public int customerid { get; set; }      /// <summary>     /// gets or sets application id.     /// </summary>     /// <value>the application id.</value>     [datamember]     public int applicationid { get; set; }      /// <summary>     /// gets or sets application.     /// </summary>     /// <value>the application.</value>     [datamember]     public applicationdto application { get; set; }      /// <summary>     /// gets or sets customer.     /// </summary>     /// <value>the customer.</value>     [datamember]     public customerdto customer { get; set; }      /// <summary>     /// gets or sets initial version id.     /// </summary>     /// <value>the initial version id.</value>     [datamember]     public int initialversionid { get; set; }      /// <summary>     /// gets or sets current version id.     /// </summary>     /// <value>the current version id.</value>     [datamember]     public int currentversionid { get; set; }      /// <summary>     /// gets or sets name of unique instance.     /// </summary>     /// <value>the name of unique instance.</value>     [datamember]     public string uniqueinstancename { get; set; } } 

let's have 2 applications present in database, , several customers.

in mvc web app, show list of applications , click link called 'create instance'.

i select customer instance for, , clicks save.

in ef code first, default saves related objects - in case application , customer.

since i'm doing using ajax calls i'm sending id of both application , customer controller. looking @ database, that's need create applicationinstance.

i 'cheat' doing this:

var appinstance = new applicationinstancedto();             appinstance.initialversionid = 1;             appinstance.currentversionid = 2;             appinstance.applicationid = 1;             appinstance.customerid = 1;             appinstance.uniqueinstancename = "test";  db.applicationinstances.add(appinstance); db.savechanges(); 

but of course, exception database telling me name column in applications table not allow null.

is there way can cheat more, , avoid entire insert of related objects?

i have foreign key relationships , such set correctly.

i recommend changing associations foreign key associations instead of being independent (which now):

public class application {     public int id { get; set; }     public string name { get; set; } }  public class customer {     public int id { get; set; }     public string name { get; set; } }  public class applicationinstance {     public int id { get; set; }     public int applicationid { get; set; }     public int customerid { get; set; }      public application application { get; set; }     public customer customer { get; set; } } 

and can save this:

var appinstance = new applicationinstance(); appinstance.customerid = customerid; appinstance.applicationid = applicationid;  db.applicationinstances.add(appinstance); db.savechanges(); 

update:

you can use foreignkeyattribute data annotation relate fk columns navigation properties:

public class applicationdto {     public int id { get; set; }     public string name { get; set; } }  public class customerdto {     public int id { get; set; }     public string name { get; set; } }  public class applicationinstancedto {     public int id { get; set; }     public int applicationid { get; set; }     public int customerid { get; set; }      [foreignkey("applicationid")]     public applicationdto application { get; set; }      [foreignkey("customerid")]     public customerdto customer { get; set; } }  

Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -