Click here to Skip to main content
15,894,343 members
Home / Discussions / C#
   

C#

 
AnswerRe: Unable to cast object of type 'System.Collections.Generic.List`1[CustomerManager.Model.Customer]' to type 'CustomerManager.Model.Customer'. Pin
Dave Kreskowiak25-Jun-14 3:14
mveDave Kreskowiak25-Jun-14 3:14 
GeneralRe: Unable to cast object of type 'System.Collections.Generic.List`1[CustomerManager.Model.Customer]' to type 'CustomerManager.Model.Customer'. Pin
uglyeyes25-Jun-14 3:20
uglyeyes25-Jun-14 3:20 
GeneralRe: Unable to cast object of type 'System.Collections.Generic.List`1[CustomerManager.Model.Customer]' to type 'CustomerManager.Model.Customer'. Pin
Dave Kreskowiak25-Jun-14 3:22
mveDave Kreskowiak25-Jun-14 3:22 
GeneralRe: Unable to cast object of type 'System.Collections.Generic.List`1[CustomerManager.Model.Customer]' to type 'CustomerManager.Model.Customer'. Pin
uglyeyes25-Jun-14 3:39
uglyeyes25-Jun-14 3:39 
GeneralRe: Unable to cast object of type 'System.Collections.Generic.List`1[CustomerManager.Model.Customer]' to type 'CustomerManager.Model.Customer'. Pin
Dave Kreskowiak25-Jun-14 3:51
mveDave Kreskowiak25-Jun-14 3:51 
GeneralRe: Unable to cast object of type 'System.Collections.Generic.List`1[CustomerManager.Model.Customer]' to type 'CustomerManager.Model.Customer'. Pin
uglyeyes25-Jun-14 3:57
uglyeyes25-Jun-14 3:57 
GeneralRe: Unable to cast object of type 'System.Collections.Generic.List`1[CustomerManager.Model.Customer]' to type 'CustomerManager.Model.Customer'. Pin
Nathan Minier25-Jun-14 4:07
professionalNathan Minier25-Jun-14 4:07 
QuestionRe: Unable to cast object of type 'System.Collections.Generic.List`1[CustomerManager.Model.Customer]' to type 'CustomerManager.Model.Customer'. Pin
Nathan Minier25-Jun-14 3:59
professionalNathan Minier25-Jun-14 3:59 
Okay, you have a little work to do on this. even when using POCOs, you need to keep the shape of the database and the limitations of SQL (like field values in tables) in mind. You still use foreign keys to map to complex types, and you cannot insert values into a SQL field that it does not recognize as a native type.

I'm assuming your context class looks like:

C#
public class MyContext : DbContext
{
   public DbSet<Customer> Customers { get; set; }
   public DbSet<Company> Companies { get; set; }
   public DbSet<Order> Orders { get; set; }
}


You have a M:M mapping for customers to companies, and and 1:M for customers and orders (I'm guessing). without seeing the Order class, it looks like our base structure has an few inherent flaws, and will not be able to normalize or even perform proper data access at the moment.

Here's the obvious changes:

C#
public class Company
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] [Display(AutoGenerateField = false)]
public int CompanyId { get; set; }

[StringLength(50)]
public string Name { get; set; }

[StringLength(200)]
public string Address { get; set; }

[StringLength(500)]
public string Email { get; set; }

[StringLength(1000)]
public string Phone { get; set; }

[StringLength(50)]
public string City { get; set; }

public State State { get; set; }
public int StateId { get; set; }
public int Zip { get; set; }

public virtual ICollection Orders{ get; set; } // Change for M:M mapping
}

public class Customer
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] [Display(AutoGenerateField = false)]
public int CustomerId { get; set; }

[StringLength(50)]
public string FirstName { get; set; }

[StringLength(50)]
public string LastName { get; set; }

[StringLength(100)]
public string Email { get; set; }

[StringLength(1000)]
public string Address { get; set; }

[StringLength(50)]
public string City { get; set; }
public int Zip { get; set; }

//State is not a valid class for the DB. If it's a Table do this
[Display(AutoGenerateField = false), ForeignKey("State")]
public int StateId { get; set; }
public virtual State State { get; set; } 

public virtual ICollection Orders{ get; set; } // Change for M:M mapping

// Gender is not a valid field class. Just use the enum string (or int) value
public string Gender { get; set; }


Okay, now that the changes are made to those classes, let's look at a possible Orders class. I'll just add the fields to wire the M:M up, I don't know what other fields you have in there. This is assuming that One Customer can have Many Orders, and One Company can have Many Orders, but each Order has only One Customer and only One Company.

C#
public class Order
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] [Display(AutoGenerateField = false)]
public int Id { get; set; }

/*
Order-specific fields here.
*/

[Display(AutoGenerateField = false), ForeignKey("Customer")]
public int CustomerId { get; set; }
[Display(AutoGenerateField = false), ForeignKey("Company")]
public int CompanyId { get; set; }

public virtual Customer Customer { get; set; }
public virtual Company Company { get; set; }

public Order(){}

public Order(Customer customer, Company company)
{
CustomerId = customer.CustomerId;
CompanyId = company.CompanyId;
}
}


My sample has a parametrized constructor, but that's optional. If you do include one, though, always include a default constructor.

Now let's swing back to the Context class and implement an Update method.
C#
public class MyContext : DbContext
{
   public DbSet<Customer> Customers { get; set; }
   public DbSet<Company> Companies { get; set; }
   public DbSet<Order> Orders { get; set; }

   public bool UpdateOrder(Order order)
   {
      if(Orders.Any(x => x.Id == order.Id))
         this.Entry(order).State = EntityState.Modified;
      else
         Orders.Add(order);
      return this.SaveChanges() != 0;
   }
   public bool UpdateCompany(Company company)
   {
      if(Companies.Any(x => x.CompanyId == company.CompanyId))
         this.Entry(company).State = EntityState.Modified;
      else
         Companies.Add(company);
      return this.SaveChanges() != 0;
   }
   public bool UpdateCustomer(Customer customer)
   {
      if(Customers.Any(x => x.CustomerId == customer.CustomerId))
         this.Entry(customer).State = EntityState.Modified;
      else
         Customers.Add(customer);
      return this.SaveChanges() != 0;
   }
   public bool DeleteOrder(Order order)
   {
      if(Orders.Any(x => x.Id == order.Id))
      {
         Orders.Remove(order);
         return this.SaveChanges() != 0;
      }
      return false;
   }
// etc...
}


Then just create objects and add them via context.Update().

modified 25-Jun-14 10:11am.

AnswerRe: Unable to cast object of type 'System.Collections.Generic.List`1[CustomerManager.Model.Customer]' to type 'CustomerManager.Model.Customer'. Pin
uglyeyes25-Jun-14 15:51
uglyeyes25-Jun-14 15:51 
QuestionHow to extract a propertybag or complex object send from VB6 through MSMQ in C# Pin
SnailsWayne24-Jun-14 22:15
SnailsWayne24-Jun-14 22:15 
AnswerRe: How to extract a propertybag or complex object send from VB6 through MSMQ in C# Pin
Dave Kreskowiak25-Jun-14 3:02
mveDave Kreskowiak25-Jun-14 3:02 
GeneralRe: How to extract a propertybag or complex object send from VB6 through MSMQ in C# Pin
SnailsWayne25-Jun-14 14:59
SnailsWayne25-Jun-14 14:59 
GeneralRe: How to extract a propertybag or complex object send from VB6 through MSMQ in C# Pin
Dave Kreskowiak25-Jun-14 17:26
mveDave Kreskowiak25-Jun-14 17:26 
GeneralRe: How to extract a propertybag or complex object send from VB6 through MSMQ in C# Pin
SnailsWayne25-Jun-14 21:33
SnailsWayne25-Jun-14 21:33 
AnswerRe: How to extract a propertybag or complex object send from VB6 through MSMQ in C# Pin
Bernhard Hiller25-Jun-14 20:29
Bernhard Hiller25-Jun-14 20:29 
GeneralRe: How to extract a propertybag or complex object send from VB6 through MSMQ in C# Pin
SnailsWayne25-Jun-14 21:33
SnailsWayne25-Jun-14 21:33 
QuestionWant place code logic in class using Inno setup Pin
Ashfaque Hussain24-Jun-14 20:31
Ashfaque Hussain24-Jun-14 20:31 
AnswerRe: Want place code logic in class using Inno setup PinPopular
Pete O'Hanlon24-Jun-14 20:40
mvePete O'Hanlon24-Jun-14 20:40 
GeneralRe: Want place code logic in class using Inno setup Pin
Ashfaque Hussain24-Jun-14 22:00
Ashfaque Hussain24-Jun-14 22:00 
QuestionCreate Microsoft Excel in C# Pin
Jassim Rahma24-Jun-14 20:22
Jassim Rahma24-Jun-14 20:22 
AnswerRe: Create Microsoft Excel in C# Pin
Kornfeld Eliyahu Peter24-Jun-14 20:29
professionalKornfeld Eliyahu Peter24-Jun-14 20:29 
AnswerRe: Create Microsoft Excel in C# Pin
Richard Deeming25-Jun-14 1:32
mveRichard Deeming25-Jun-14 1:32 
SuggestionRe: Create Microsoft Excel in C# Pin
LucasRLS25-Jun-14 1:57
LucasRLS25-Jun-14 1:57 
AnswerRe: Create Microsoft Excel in C# Pin
Nguyen.H.H.Dang25-Jun-14 18:45
professionalNguyen.H.H.Dang25-Jun-14 18:45 
QuestionDbContext recreate with cache clear Pin
Nathan Minier24-Jun-14 3:25
professionalNathan Minier24-Jun-14 3:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.