Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Relations to extended entitie! #3242


User avatar
0
pointlevel created

Hi!

I have extended the OrganizationUnit entity with a couple of properties. But i also would like to relate this entity to another one by both one-to-one and one-to-many relationship. Is this possible? I have this extendend class:

public class ExOu : OrganizationUnit
    {
        public virtual string Adress { get; set; }
        public virtual string Email { get; set; }
        public virtual string Phone { get; set; }

        public virtual Setting Setting { get; set; }

        public virtual ICollection<Customer> Customers { get; set; }
    }

    public class Setting : FullAuditedEntity<long>
    {
         public virtual bool UseSetting1 { get; set; }
         public virtual bool UseSetting2 { get; set; }
         
         public virtual long OrganizationUnitId { get; set; }
         [ForeignKey("OrganizationUnitId")] // Doesn´t work
         public virtual ExOu as ExOu { get; set; } 
    }

    public class Customer : FullAuditedEntity<long>
    {
         public virtual string FirstName { get; set; }
         public virtual string LastName { get; set; }

         public virtual long OrganizationUnitId { get; set; }
         [ForeignKey("OrganizationUnitId")] // This Doesn´t work
         public virtual ExOu ExOu { get; set; }
    }

5 Answer(s)
  • User Avatar
    0
    alirizaadiyahsi created

    Hi,

    Actually, this question is about EF. And yes, you can do what you want. EF supports this. BTW, what you mean when you say "This doesn't work"?

    Anyway, could you try to do this with using fluent api? Check this: <a class="postlink" href="http://stackoverflow.com/questions/35506158/one-to-one-relationships-in-entity-framework-7-code-first">http://stackoverflow.com/questions/3550 ... code-first</a>

  • User Avatar
    0
    blewis created

    Did you get this working? I too must extend the OU class, adding a bunch of custom fields as well as creating one-to-many relationships with other new classes that I am creating. If the ForeignKey attribute did not work, did using the Fluent API take care of this? Just starting this new project, so any tips would be appreciated.

    thanks, Bryan

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @blewis,

    Do you use EF core or EF ?

    Thanks.

  • User Avatar
    0
    blewis created

    I am using EF CORE on the Asp.Net Core + jQuery template.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @blewis,

    I haven't tried this with EF core but I'm sure there must be a way. Have you tried it (<a class="postlink" href="https://docs.microsoft.com/en-us/ef/core/modeling/relational/fk-constraints">https://docs.microsoft.com/en-us/ef/cor ... onstraints</a>) ? If you have an error, we can try to help.

    Thanks.