EF 4.1 - Model Relationships

前端 未结 6 510
情书的邮戳
情书的邮戳 2021-01-03 21:06

I\'m trying to create a quick ASP.NET MVC 3 application using the RC version of EF 4.1. I have two models:

public class Race
{
    public int RaceId { get; s         


        
6条回答
  •  星月不相逢
    2021-01-03 22:05

    It recognizes Id as the primary key by convention. So what you need to do:

    public class Race
    {
        [Key]
        public int RaceId { get; set; }
        public string RaceName { get; set; }
        public string RaceDescription { get; set; }
        public DateTime? RaceDate { get; set; }
        public decimal? Budget { get; set; }
        public Guid? UserId { get; set; }
        public int? AddressId { get; set; }
    
        public virtual Address Address { get; set; }
    }
    and
    
    public class Address
    {
        [Key]
        public int AddressId { get; set; }
        public string Street { get; set; }
        public string StreetCont { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string ZipCode { get; set; }
    
        [ForeignKey("RaceId")] // Maybe telling it what the ForeignKey is will help?
        public virtual Race Race { get; set; }
    }
    

    The [Key] attribute indicates that it should be the PrimaryKey

    If you don't want this, you need to rename your primary keys to simply public int Id {get; set; }

提交回复
热议问题