locked
How to annotate EF model class for the equivalent of HasMany, WithOptional and HasForeignKey RRS feed

  • Question

  • User1183902823 posted

    see the full below code.

    public partial class TestModel : DbContext
        {
            public TestModel()
                : base("name=TestModel")
            {
            }
    
            public virtual DbSet<Department> Departments { get; set; }
            public virtual DbSet<Employee> Employees { get; set; }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Department>()
                    .Property(e => e.DepartmentName)
                    .IsUnicode(false);
    
                modelBuilder.Entity<Department>()
                    .HasMany(e => e.Employees)
                    .WithOptional(e => e.Department)
                    .HasForeignKey(e => e.DeptID);
    
                modelBuilder.Entity<Employee>()
                    .Property(e => e.Name)
                    .IsUnicode(false);
            }
        }
        public partial class Employee
        {
            [DatabaseGenerated(DatabaseGeneratedOption.None)]
            public int ID { get; set; }
    
            public string Name { get; set; }
    
            public int? DeptID { get; set; }
    
            public virtual Department Department { get; set; }
        }
    
        public partial class Department
        {
            public Department()
            {
                Employees = new HashSet<Employee>();
            }
    
            [DatabaseGenerated(DatabaseGeneratedOption.None)]
            public int ID { get; set; }
    
            public string DepartmentName { get; set; }
    
            public virtual ICollection<Employee> Employees { get; set; }
        }

    in the above code relation has been set by fluent configuration. they use HasMany, WithOptional and HasForeignKey. i like to know without fluent configuration how could i achieve the same by annotation at class level.

    please show me with a class level example. thanks

    Tuesday, December 12, 2017 11:53 AM

Answers

  • User-821857111 posted

    The following will achieve the same configuration using attributes instead of the Fluent API:

    public partial class Employee
    {
        DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int ID { get; set; }
    
        public string Name { get; set; }
    
        public int? DeptID { get; set; }
        [ForeignKey("DeptID")]
        public virtual Department Department { get; set; }
    }
    
    public partial class Department
    {
        public Department()
        {
            Employees = new HashSet<Employee>();
        }
    
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int ID { get; set; }
    
        public string DepartmentName { get; set; }
    
        public virtual ICollection<Employee> Employees { get; set; }
    }

    http://www.learnentityframeworkcore.com/configuration/data-annotation-attributes/foreignkey-attribute

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 13, 2017 7:19 AM
  • User-821857111 posted

    There are no annotation equivalents for the HasMany or WithOptional methods. The ForeignKey attribute is equivalent to the HasForeignKey method. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 18, 2017 8:12 AM

All replies

  • User-821857111 posted

    The following will achieve the same configuration using attributes instead of the Fluent API:

    public partial class Employee
    {
        DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int ID { get; set; }
    
        public string Name { get; set; }
    
        public int? DeptID { get; set; }
        [ForeignKey("DeptID")]
        public virtual Department Department { get; set; }
    }
    
    public partial class Department
    {
        public Department()
        {
            Employees = new HashSet<Employee>();
        }
    
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int ID { get; set; }
    
        public string DepartmentName { get; set; }
    
        public virtual ICollection<Employee> Employees { get; set; }
    }

    http://www.learnentityframeworkcore.com/configuration/data-annotation-attributes/foreignkey-attribute

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 13, 2017 7:19 AM
  • User1183902823 posted

    thanks for your reply

    modelBuilder.Entity<Department>()
                    .HasMany(e => e.Employees)
                    .WithOptional(e => e.Department)
                    .HasForeignKey(e => e.DeptID);

    tell which annotation represent HasMany in model class ?

    tell which annotation represent WithOptional in model class?

    tell which annotation represent HasForeignKey in model class?

    thanks

    Wednesday, December 13, 2017 8:46 AM
  • User-821857111 posted

    There are no annotation equivalents for the HasMany or WithOptional methods. The ForeignKey attribute is equivalent to the HasForeignKey method. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 18, 2017 8:12 AM