2014-06-07 1 views
0

코드 첫 번째 마이그레이션에서 추가 필드가 생성되는 문제와 원하지 않는 관계가 발생합니다. 나는 기본 모델, LotteryCarts이코드에 처음으로 추가 키가 생성되는 경우

[Table("lottery_carts")] 
public class LotteryCart : Auditable 
{ 
    public LotteryCart() 
    { 
     LotteryCartItems = new List<LotteryCartItem>(); 
    } 
    //stored in banner: no 
    [Key] 
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] 
    public int Id { get; set; } 

    [Required(ErrorMessage = "A lottery is required.")] 
    public int LotteryId { get; set; } 
    public virtual Lottery Lottery { get; set; } 

    public virtual List<LotteryCartItem> LotteryCartItems { get; set; } 
} 

및 LotteryCartItem

LotteryCartItem 그냥 '나는 돈 몇 가지 이유를 들어

(일부 추가 메타 데이터) LotteryCart와 클래스 사이에 결합되어

public class LotteryCartItem 
{ 

    [Key, Column(Order=0)] 
    public int ClassId { get; set; } 
    public virtual Class Class { get; set; } 

    [Key, Column(Order = 1)] 
    public int LotteryCartId { get; set; } 
    public virtual LotteryCart LotteryCart { get; set; } 

    public int BidWeight { get; set; } 
    public int order { get; set; } 
    public string type { get; set; } 
} 

코드를 처음 이해하면 추가 필드가 생성됩니다.

 CreateTable(
      "dbo.LotteryCartItems", 
      c => new 
       { 
        ClassId = c.Int(nullable: false), 
        LotteryCartId = c.Int(nullable: false), 
        BidWeight = c.Int(nullable: false), 
        order = c.Int(nullable: false), 
        type = c.String(), 
       }) 
      .PrimaryKey(t => new { t.ClassId, t.LotteryCartId }) 
      .ForeignKey("dbo.classes", t => t.ClassId, cascadeDelete: true) 
      .ForeignKey("dbo.lottery_carts", t => t.LotteryCartId, cascadeDelete: true) 
      .Index(t => t.ClassId) 
      .Index(t => t.LotteryCartId); 

     AddColumn("dbo.lottery_carts", "Class_Id", c => c.Int()); 
     AddColumn("dbo.lottery_carts", "Class_Id1", c => c.Int()); 
     AddForeignKey("dbo.lottery_carts", "Class_Id", "dbo.classes", "Id"); 
     AddForeignKey("dbo.lottery_carts", "Class_Id1", "dbo.classes", "Id"); 

class_Id와 Class_Id1이 lottery_carts에있는 이유는 확실치 않습니다. 거기에는 직접적인 관계가없는 것처럼 보입니다.

미리 도움 주셔서 감사합니다.

+0

제대로 정의해야 하는가? –

+0

글쎄, 기본 키 필드는 nullable이 될 수 없습니다! –

+0

@AppDeveloper 버전 5 와우, 내가 바꿨다! 아직도 기쁨이 없습니다. 나는 원래 전용 키 필드를 가지고 있으며 그것이 필요하지 않다는 것을 깨달았을 때 null을 잊어 버렸습니다! –

답변

0

LotteryCartItem 사용하고있는 EF의 버전

public class LotteryCartItem 
{ 

    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.None)] 
    [Key, Column(Order=0), ForeignKey("Class")] 
    public int ClassId { get; set; } 
    public virtual Class Class { get; set; } 

    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.None)] 
    [Key, Column(Order=1), ForeignKey("LotteryCart")] 
    public int LotteryCartId { get; set; } 
    public virtual LotteryCart LotteryCart { get; set; } 

    public int BidWeight { get; set; } 
    public int order { get; set; } 
    public string type { get; set; } 
} 
+0

나를 도와 줘서 고마워! –

관련 문제