Entity Framework 1:1 Relationship Identity Colums

Posted by Hupperware on Stack Overflow See other posts from Stack Overflow or by Hupperware
Published on 2012-06-08T20:21:41Z Indexed on 2012/06/08 22:40 UTC
Read the original article Hit count: 220

Filed under:
|

I cannot for the life of me get two separate tables to save with a relationship and two identity columns. I keep getting errors like A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'OlsTerminalId'. I can't imagine that this setup is abnormal.

[DataContract]
    public class OlsData {
        private static readonly DateTime theUnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);

    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int OlsDataId { get; set; }

    [DataMember(Name = "id")]
    public int Id { get; set; }

    public DateTime Timestamp { get; set; }

    [DataMember(Name = "created")]
    [NotMapped]
    public double Created {
        get { return (Timestamp - theUnixEpoch).TotalMilliseconds; }
        set { Timestamp = theUnixEpoch.AddMilliseconds(value); }
    }

    [InverseProperty("OlsData")]
    [DataMember(Name = "terminal")]
    public virtual OlsTerminal OlsTerminal { get; set; }
}

[DataContract]
public class OlsTerminal {
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int OlsTerminalId { get; set; }

    public int OlsDataId { get; set; }

    [DataMember(Name = "account")]
    [NotMapped]
    public virtual OlsAccount OlsAccount { get; set; }

    [DataMember(Name = "terminalId")]
    public string TerminalId { get; set; }

    [DataMember(Name = "merchantId")]
    public string MerchantId { get; set; }

    public virtual OlsData OlsData { get; set; }
}

 public class OlsDataContext : DbContext {
        protected override void OnModelCreating(DbModelBuilder aModelBuilder) {
            aModelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }

        public OlsDataContext(string aConnectionString) : base(aConnectionString) {}
        public DbSet<OlsData> OlsData { get; set; }
        public DbSet<OlsTerminal> OlsTerminal { get; set; }
        public DbSet<OlsAccount> OlsAccount { get; set; }

    }

© Stack Overflow or respective owner

Related posts about c#

Related posts about entity-framework