in Education by
I have two entities: Job and Orderable on our code-first database: The (extremely simple) models of these two are: class Job { public Guid Id {get; set;} public string JobName {get; set;} public List Items {get; set;} } class Orderable { public Guid Id {get; set;} public string MaterialNumber {get; set;} } I wanted to be able to reference the Job from the orderable, so I added a property like so: public Job OwningObject {get; set;} But when I did the migration, it created a second foreign key for this object. Now, it seems that if I am getting the Orderables from the Job, it uses key1, but if I am getting the Job from the Orderable, it uses key2. Within our OnmodelCreating method in our context we have added the following before the key was created, which I thought was required to make it work, but I am not sure if this is potentially causing the issue. modelBuilder.Entity().HasMany(x => x.Items).WithOne(); We were able to force the key to reference the proper field with a data annotation of: [ForeignKey("OwningObjectId1")] But this feels hacky and I don't like it as a permanent solution. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
EF uses shadow properties to persist the foreign key relationship when no explicit foreign key property is applied. The best thing to do is simply have explicit foreign key properties: [ForeignKey(nameof(OwningObject)] public Guid OwningObjectId { get; set; } public Job OwningObject { get; set; } If you're going to rely on shadow properties, then you need to actually think through what you're doing. First, when you use WithOne(), with no param, it's going to create a shadow property based on the related entity, which is Job. As a result, you'll get a column, JobId. When you have a reference property, it's going to assume a foreign key column based on the name of that property: OwningObjectId. Essentially, the combination of a reference property and using WithOne() and not referencing that property in the param, actually creates two separate one to many relationships. This can be easily corrected by simply telling EF which property to use explicitly: modelBuilder.Entity().HasMany(x => x.Items).WithOne(x => x.OwningObject);

Related questions

0 votes
    I have an issue refrerencing an assembly in a view for MVC. This namespace is part of a dll I ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 26, 2022 in Education by JackTerrance
0 votes
    I have an issue refrerencing an assembly in a view for MVC. This namespace is part of a dll I ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I am developing a .NET core application with Identity login which provides a Bootstrap form In the /Identity ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    Does Server Core 2008 support asp.net? I see references online saying that it isn't supported, but they ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 25, 2022 in Education by JackTerrance
0 votes
    Does Server Core 2008 support asp.net? I see references online saying that it isn't supported, but they ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    Does Server Core 2008 support asp.net? I see references online saying that it isn't supported, but they ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    I have an ASP.Net core 1.1 website and I want to embed Power BI reports into the site. Azure ... JsonConvert.DeserializeObject(result); Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    I am trying to update an Entity EF Core record like this: public class Customer { public int Id { ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    In ASP.NET, if I databind a gridview with a array of objects lets say , how can I retrieve and ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    I have an ASP.NET MVC website. In my backend I have a table called People with the following ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Aug 1, 2022 in Education by JackTerrance
0 votes
    I have implemented exception handling as mentioned in below link How to pass error message to error view in ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    The RangeValidator will fetch the value of the input and validate it with regex. Part of the regex is ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 16, 2022 in Education by JackTerrance
0 votes
    My thinking is that people use Docker to be sure that local environment is the same as production and that ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 12, 2022 in Education by JackTerrance
0 votes
    i am using serilog in asp net core application and using a json formatter to create a daily log file ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 1, 2022 in Education by JackTerrance
0 votes
    I'm fairly new to Razor Components and I would like to change the title and other data inside the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 26, 2022 in Education by JackTerrance
...