Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Copilot Lvl 2
Message 1 of 1

C# .NetCore 2.2 foreign key class generates new id everytime i change/save main class contact

Hello, 

I working on my first C# .NetCore web APP with razorpages and slightly more advanced DB scheme, which is because of security rules (users data cannot be in 1 Database Table, so it is referrenced to another tables via FKs)

 

So basically I have 1 main DB table scaffolded from main Contact class:

 

public class Contact
    {
        [Key]
        public int ID { get; set; }
        public string Surename{ get; set; }
        public string FirstName{ get; set; }
        public int? Location_ID { get; set; }
        [ForeignKey("Location_ID")]
        public Location Location{ get; set; }
        public string Degree{ get; set; }
        public string Phone { get; set; }
        public int? PhonePrivate_ID { get; set; } 
        [ForeignKey("PhonePrivate_ID ")]
        public PhonePrivate PhonePrivate { get; set; }
        public string SECtel { get; set; }
}

And other related tables with usefull values for contacts (Locations, Assignments, Addresses of main offices, etc.) are defined like this problematic PhonePrivate:

public class PhonePrivate
    {
        [Key]
        public int PhonePrivate_ID { get; set; }
        public string Number { get; set; }
        public bool Active { get; set; }
    }

I have Fluent API overides fot another values table for instance Locality:

modelBuilder.Entity<Locality>().HasMany(a => a.Contact).WithOne(b => b.Locality);

and when creating contact with blank(null) value of Locality_ID, there is no new record in Locality Table.

 

But when is blank/Null PhonePrivate_ID, everytime is new contact saved that spawns new record in Table PhonePrivate which appears with ID+1 and value from create/update form (null or number)?

 

Is there any fluent APi anotation, which determine, that the value from Contact table doesn't have to value (or relationship) to a table PhonePrivate ? I supposed that declaration in form:

public int? PhonePrivate_ID { get; set; } 

is used for mandatory attributes...

 

Thank you very much for any kind of help

 

Best regards.