none
Annotation sytax to Fluent: Composite Primary Key using Foreign Key RRS feed

  • Question

  • I've been struggling to determine if/how I could "translate" the following entity from using dataannotations to fluent api.  The below works fine, but I have trying to define the same configuration using Fluent only and i'm not sure it is possible.  Anyone know if this is possible?

        [DataContract]
        [KnownType(typeof(Computer))]
        public class Service
        {

            [DataMember][Key,ForeignKey("Computer")][Column(Order = 0)]
            public int ComputerID { get; set; }

            [DataMember][Key][Column(Order = 1)]
            public string KeyName { get; set; }

            [DataMember] public string DisplayName { get; set; }
            [DataMember] public string ImagePath { get; set; }
            [DataMember] public string ObjectName { get; set; }
            [DataMember] public int Start { get; set; }
            [DataMember] public int Type { get; set; }

            [DataMember] public virtual Computer Computer { get; set; }
        }


    ck

    Thursday, February 9, 2017 10:54 PM

Answers

  • Hi ckJustReading,

    Please refer to the following fluent API.

    // Composite primary key 
                modelBuilder.Entity<Service>()
                .HasKey(d => new { d.ComputerID, d.KeyName });
    
                //foreign key
    
                modelBuilder.Entity<Service>().HasRequired(c => c.Computer)
                    .WithMany(d => d.Services)
                    .HasForeignKey(d => d.ComputerID);

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by ckJustReading Friday, February 10, 2017 9:14 PM
    Friday, February 10, 2017 6:14 AM
    Moderator

All replies

  • Hi ckJustReading,

    Please refer to the following fluent API.

    // Composite primary key 
                modelBuilder.Entity<Service>()
                .HasKey(d => new { d.ComputerID, d.KeyName });
    
                //foreign key
    
                modelBuilder.Entity<Service>().HasRequired(c => c.Computer)
                    .WithMany(d => d.Services)
                    .HasForeignKey(d => d.ComputerID);

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by ckJustReading Friday, February 10, 2017 9:14 PM
    Friday, February 10, 2017 6:14 AM
    Moderator
  • That was it.  Thank you!

    ck

    Friday, February 10, 2017 9:15 PM