Javad Azimi
Javad Azimi

Reputation: 95

relationship with Entity Framework 5 and .net MVC 4

I have a model like this :

namespace HiTech.Common.Models{
    public class CodeContext : DbContext
    {
        public CodeContext(): base("DefaultConnection"){}

        public DbSet<Code> Codes { get; set; }
        public DbSet<CodeMember> CodeMembers { get; set; }
    }
    public class Code
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        public string Description { get; set; }
        public string EnDescription { get; set; }
        public virtual ICollection<CodeMember> CodeMembers { get; set; }
    }

    public class CodeMember
    {

        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        public string Description { get; set; }
        [Required]
        public int CodeId { get; set; }
        [ForeignKey("CodeId")]
        public Code Code { get; set; }    }}

and I use CodeMember in there like this :

namespace HiTech.Sales.Customers.Models{
    public class CustomerContext : DbContext
    {
        public CustomerContext()
            : base("DefaultConnection")
        {

        }
        public DbSet<Customer> Customers { get; set; }
    }
    public class Customer
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        [Required]

        public string CustCode { get; set; }

        [ForeignKey("IndividualId")]
        public CodeMember Individual { get; set; }
        public int IndividualId { get; set; }
    } 
}

My questions are:

  1. how I filter Individual from Code Id in my model (Not in controller like below code):

    public ActionResult Create()
    {
        ViewBag.IndividualId = new SelectList(
               db.CodeMembers.Where(p => p.Code.EnDescription == "Individual"),
                   "Id", "Description");
        return View();
    }
    
  2. when I put CustomerModel in another Area , I can not load or save data with the followed error :

Exception Details: System.Data.SqlClient.SqlException: Invalid object name dbo.Customers'.
Source Error:
Line 56: {
Line 57: db.Customers.Add(customer);
Line 58: db.SaveChanges();
Line 59: return RedirectToAction("Index");
Line 60:

Upvotes: 0

Views: 143

Answers (1)

Gert Arnold
Gert Arnold

Reputation: 109185

Starting with your second question, the non-existent table. That is because you have two contexts that both know only parts of the complete data model. You can work with such bounded contexts, but there should be one context that "knows" all, so it is able to generate the whole database.

As for the first question. If you mean if it's possible to combine DbSets from different contexts in one (SQL) query the answer is no. If you want to create a query that combines Customer with Code through CodeMember you'll have to query the Code + CodeMember in one query and the Customer in a second query. But you can take a CodeMember from a CodeContext and attach it to a CustomerContext (make sure to detach it from the CodeContext first).

Upvotes: 0

Related Questions