1

私のEFコードの最初の例で関係を作成する方法を誰かに教えてもらえますか-Product_Specクラスと多くの関係を持つProductsクラスの関係が必要なので、コードをコンパイルすると、データベースが生成されたときに関係があります。 Product_Spec データ コンテキスト クラスに関連する仕様クラスの関係

クラス:

namespace MvcApplication1.Models
{
    public class Department
    {
        [Key]
        public long Id { get; set; }

        [Required(ErrorMessage="Please enter a name for the departments.")]
        [DataType(DataType.Text)]
        public string Name { get; set; }

        [DataType(DataType.Text)]
        [Required(ErrorMessage = "Please enter a valid url for the department.")]
        public string Url { get; set; }

        public virtual List<Product> Products { get; set; }
    }


    public class Product
    {
        [Key]
        public long Id { get; set; }
        [ForeignKey("FK_Department_Id")]
        public long DepartmentId { get; set; }
        [DataType(DataType.Text)]
        public string Title { get; set; }

        [DataType(DataType.Currency)]
        public decimal UnitPrice { get; set; }
        [DataType(DataType.Currency)]
        public decimal SellPrice { get; set; }               

    }

    public class Product_Spec
    {        
        [ForeignKey("FK_Spec_ProductId")]
        public long ProductId { get; set; }

        [ForeignKey("FK_Spec_SpecId")]
        public long SpecId { get; set; }        
    }

    public class Specification
    {
        [Key]
        public long SpecId { get; set; }
        [DataType(DataType.Text)]
        [Required(ErrorMessage = "Please enter a product specification type.")]
        public string Type { get; set; }
        [DataType(DataType.Text)]
        [Required(ErrorMessage = "Please enter a product specification value.")]
        public string Value { get; set; }

    }


}


namespace MvcApplication1
{

    public class DataContext : DbContext
    {
        public DbSet<Department> Department { get; set; }
        public DbSet<Product> Product { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Department>().HasRequired(x => x.Products)
                .WithMany().HasForeignKey(x => x.Id).WillCascadeOnDelete(true);

            modelBuilder.Entity<Product>().HasOptional(x => x.Product_Specs)
                .WithMany().HasForeignKey(x =>x.ProductId) // this lines doesn't work

            base.OnModelCreating(modelBuilder);
        }
    }
}
4

2 に答える 2

0

ForeignKey制約名ではなく、属性に列名を設定する必要があると思います:

public class Product
{
    [Key]
    public long Id { get; set; }
    public long DepartmentId { get; set; }

    [DataType(DataType.Text)]
    public string Title { get; set; }

    [DataType(DataType.Currency)]
    public decimal UnitPrice { get; set; }

    [DataType(DataType.Currency)]
    public decimal SellPrice { get; set; } 

    [ForeignKey("DepartmentId")]
    public virtual Department Department { get; set; }

    public ICollection<Product_Spec> ProductSpecs { get; set; }
}

public class Product_Spec
{                
    public long ProductId { get; set; }        
    public long SpecId { get; set; }

    [ForeignKey("ProductId")]
    public virtual Product Product {get; set;}        
}
于 2013-08-05T06:27:58.357 に答える