ASP.NET MVC5 - unwanted field created after update database - c#

I'm trying to create a 'one to many' relation between the classes 'ApplicationUser' and one recently created called 'Issue'.
So, in Models / IdentityModels.cs / ApplicationUser i added this property:
public ICollection<Issue> Issues { get; set; }
And Issue.cs has this code:
namespace Test.Models
{
public class Issue
{
public int Id { get; set; }
public ApplicationUser Courier { get; set; }
public ApplicationUser Customer { get; set; }
}
}
I'm using automatic migrations. So, after building and running 'update-database', the Issues table was created with these fields:
Id
ApplicationUser_Id
CourierId
CustomerId
My question is why was the field 'ApplicationUser_Id ' created and how can i prevent it?

The problem is that EF thinks you actually want three one-to-many relationships between Issue and ApplicationUser:
one for ICollection<Issue> Issues on ApplicationUser (ApplicationUser_Id)
one for ApplicationUser Courier on Issue (CourierId)
one for ApplicationUser Customer on Issue (CustomerId)
(Note that EF allows to define relationships from either side.)
If you want that ApplicationUser.Issues contains all Issues of this User, whether he is a Courier or Customer, you will need the additional ApplicationUser_Id key. Configuring EF so that this works will be quite a pain.
Maybe a simpler solution will do: introduce two collections on ApplicationUser.
public ICollection<Issue> CourierIssues { get; set; }
public ICollection<Issue> CustomerIssues { get; set; }
And then configure the backlinks in the ModelBuilder using the fluent API to eliminate the ApplicationUser_Id key:
modelBuilder.Entity<ApplicationUser>().HasMany(au => au.CourierIssues).WithOptional(i => i.Courier);
modelBuilder.Entity<ApplicationUser>().HasMany(au => au.CustomerIssues).WithOptional(i => i.Customer);

Because you have two foreign keys to ApplicationUser, Courier and Customer, but only one collection referencing Issue on ApplicationUser. EF has no way of know which foreign key it should line up with, so it just created a new one. To handle this properly, you need to utilize fluent config:
public class ApplicationUser
{
...
public class Mapping : EntityTypeConfiguration<ApplicationUser>
{
HasMany(m => m.Issues).WithRequired(m => m.Customer);
}
}
Then, in your context:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Configurations.Add(new ApplicationUser.Mapping());
}
The problem here of course, is that you are likely wanting to track collections for both the Customer and Courier collections. For that, you need two collections:
public virtual ICollection<Issue> CustomerIssues { get; set; }
public virtual ICollection<Issue> CourierIssues { get; set; }
Then, the following fluent config:
HasMany(m => m.CustomerIssues).WithRequired(m => m.Customer);
HasMany(m => m.CourierIssues).WithRequired(m => m.Courier);

Related

Entity Framework : Invalid column name *_ID even with fluent api or data annotations

Odd issue that I've been looking at all day. I am working with Entity Framework 6. The issue I have is that I have three entities:
public partial class Order : ILocationBearingObject
{
public int Id { get; set; }
// other properties and relationships here
public int? OrderProfileId { get; set; }
public int OrderTemplateId { get; set; }
public virtual OrderProfile Profile { get; set; } // optional property
public virtual OrderTemplate OrderTemplate{ get; set; }
}
public class OrderProfile
{
public int Id { get; set; }
// other properties
// added here 6/15/2021
public virtual OrderTemplate OrderTemplate{ get; set; }
}
public class OrderTemplate : EntityMetaData
{
public int Id { get; set; }
// other properties
public int? OrderProfileId{ get; set; }
public OrderProfile OrderProfile { get; set; }
}
In our model builder, we have these definitions:
modelBuilder.Entity<Order>()
.HasOptional(x => x.OrderProfile)
.WithMany(x => x.Orders)
.HasForeignKey(x => x.OrderProfileId);
modelBuilder.Entity<OrderProfile>()
.HasOptional(x => x.OrderTemplate)
.WithOptionalPrincipal(x => x.OrderProfile);
But even with the above fluent api model, we get the error
Invalid column name 'OrderProfile_Id'
Throughout various testing I was unable to find why this issue was occurring, so I looked at our logs and found when this error started popping it's head up and then was able to find the changes associated to OrderProfile and found that the only change that was made was adding the relationship from OrderProfile to OrderTemplate.
When I removed that fluent api relationship OrderProfile to OrderTemplate, it worked as expected... I don't need that relationship to OrderTemplate, but would like it to be there, how can I establish a optional 1 to optional 1 relationship without breaking other relationships? Also, why would additional relationships be effected by this?
UPDATE 6/15/2021
So I found I had a reverse navigation property in the OrderProfile model:
public virtual OrderTemplate OrderTemplate{ get; set; }
removing that and the associated fluent relationship
modelBuilder.Entity<OrderProfile>()
.HasOptional(x => x.OrderTemplate)
.WithOptionalPrincipal(x => x.OrderProfile);
Doing the above resolved the issue, but for some reason, the issue seems to have cascaded down to another relationship that has a circular reference like the above. The Order class is involved with this cascaded issue. I guess this is a pretty big cause for concern since this application worked fine for the last 4 years and for these relationships to be decaying like this is worrisome. Does anyone know why this is happening?
if you use the right naming convention, EF will do magic. in this sample, you don't need fluent API to relate entities.
public partial class Order : ILocationBearingObject
{
public int Id { get; set; }
public int? OrderProfileId { get; set; } //means HasOptional (nullable) and ForeignKey
//variable name must be OrderProfile not Profile
public virtual OrderProfile OrderProfile { get; set; }
}
public class OrderProfile
{
public OrderProfile()
{
Orders = new HashSet<Order>();
}
public int Id { get; set; }
//be aware circular reference at any conversion or mapping
public virtual ICollection<Order> Orders {get; set;} //means WithMany
}
I've got an error like this too. It's caused by unmatching OrderProfileId property in OrderTemplate class with the fluent api model
If I'm not wrong, you want the OrderProfile model a many to many relation between Order and OrderTemplate. Then if it was the case, add the nvaigation property in OrderProfile.
public class OrderProfile
{
public int Id { get; set; }
// other properties
public virtual ICollection<Order> Orders { get; set; }
public virtual OrderTemplate OrderTemplate { get; set; }
}
Then change the fluent api model to be like this
// the EF has modelled the relation for normal 1 to many relation
// modelBuilder.Entity<Order>()
// .HasOptional(x => x.OrderProfile)
// .WithMany(x => x.Orders)
// .HasForeignKey(x => x.OrderProfileId);
modelBuilder.Entity<OrderTemplate>()
.HasOptional(x => x.OrderProfile)
.WithOptional(x => x.OrderTemplate);
You're working database-first, which always leaves room for a mismatch between the actual database model and the model EF infers from class and property names and mapping code (= conceptual model). If this happens, it may help to make EF generate a database from the conceptual model and see where it creates the column it expects, OrderProfile_Id.
This is what you'll see when logging the SQL statements:
CREATE TABLE [dbo].[OrderTemplates] (
[Id] [int] NOT NULL IDENTITY,
[OrderProfileId] [int],
[OrderProfile_Id] [int],
CONSTRAINT [PK_dbo.OrderTemplates] PRIMARY KEY ([Id])
)
...
ALTER TABLE [dbo].[OrderTemplates]
ADD CONSTRAINT [FK_dbo.OrderTemplates_dbo.OrderProfiles_OrderProfile_Id]
FOREIGN KEY ([OrderProfile_Id]) REFERENCES [dbo].[OrderProfiles] ([Id])
There you see the expected nullable column OrderProfile_Id which is the FK to OrderProfiles. It's noteworthy to see that EF does not use OrderProfileId as a foreign key field. It's just a field that could be used for anything.
That's because EF6 doesn't support 1:1 associations as foreign key associations (reference property and primitive FK property).
Knowing this, the remedy is simple: remove the property OrderTemplate.OrderProfileId and tell EF to use the field OrderTemplate.OrderProfileId in the database:
modelBuilder.Entity<OrderProfile>()
.HasOptional(x => x.OrderTemplate)
.WithOptionalPrincipal(x => x.OrderProfile)
.Map(m => m.MapKey("OrderProfileId"));
That said, I wonder why Order has a foreign key to OrderProfile. Isn't its OrderProfile determined by its OrderTemplate? If it's a redundant relationship it may be better to remove it.

How to create relationships between models that are found on different DbContext?

I am developing an application following the DDD pattern.
I have the following contexts for employee management and user account management called
EmployeeManagementContext
and
UserAccountManagementContext
Both contexts are on a separate project.
The project for employee management has the following models.
public class Employee
{
public int Id { get; private set; }
public string Name { get; private set; }
public DateTime DateOfBirth { get; private set; }
}
The project for user account management has the following models.
public class Employee
{
public int Id { get; private set; }
public string Name { get; private set; }
}
public class UserAccount
{
public int Id { get; private set; }
public string Username { get; private set; }
public string Password { get; private set; }
}
EmployeeManagementContext
public class EmployeeManagementContext : DbContext
{
public DbSet<Employee> Employees { get; set; }
}
UserAccountManagementContext
public class UserAccountManagementContext : DbContext
{
public DbSet<UserAccount> UserAccounts { get; set; }
}
I can successfully migrate both context by having different context keys but the problem is I loose the relationship between the Employee and UserAccount models.
Basically, the business rules that I need to implement between the two models are as follow:
An Employee may or may not have a UserAccount.
A UserAccount is owned by exactly one Employee only.
This means that I should have a one to zero-or-one relationship between Employee and UserAccount like the diagram below.
Please disregard the wrong relationship notation, its seems to be a limitation of the tool I am using but it is a one-to-zero-or-one relationship I assure you.
I tried the following configurations in UserAccount project:
public class UserAccountConfiguration : EntityTypeConfiguration<UserAccount>
{
HasKey(x => x.Id);
Property(x => x.Id)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
}
public class EmployeeConfiguration : EntityTypeConfiguration<Employee>
{
HasKey(x => x.Id);
Property(x => x.Id)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
HasOptional(x => x.UserAccount)
.WithRequired(x => x.Employee);
}
public class UserAccountManagementContext : DbContext
{
public DbSet<UserAccount> UserAccounts { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new UserAccountConfiguration());
modelBuilder.Configurations.Add(new EmployeeConfiguration());
base.OnModelCreating(modelBuilder);
}
}
The above configurations result to an error because Employee table already exist because of the EmployeeManagementContext in employee management project.
If I try to add the following configuration in EmployeeConfiguration of employee management project,
ToTable("Users");
EF doesn't complain anymore and creates a Users table which then creates the relationship I need between Employee and UserAccount. But the problem is, if I try to query Employee/Users in UserAccountManagementContext, it doesn't contain anything and I don't think its good design to keep on creating smaller tables that is derived from the orginal table because it will only clutter the database, right?...
I would greatly appreciate your help, thanks.
You should focus more on the domain and less on the database.
From what I can see, you have two Aggregates (Employee and UserAccount), in possible 2 bounded contexts (I can't name them as I don't have enough data). In general it's not recommended to force any invariant in a strongly consistent manner between the two Aggregates but there are exceptions. They may be as well in different databases, having different technologies. Let's now see how you can enforce the two invariants:
An Employee may or may not have a UserAccount.
This can be modeled with a nullable UserAccountId on a Employee, without any low level database references. Depending on the business rules, when an UserAccound is deleted (if this is a valid business operation on it), using a Saga/Process manager, you can set to null the corresponding UserAccountId in the Employee that had this account.
A UserAccount is owned by exactly one Employee only.
The simplest way to enforce this invariant is of technological nature: create an unique index on the UserAccountId. Other solutions imply using Sagas but are not as good as this one, for example would permit for a short period of time for the invariant to be broken.

EF re-adding FK columns after adding ICollection navigation property

I have the following classes, that already have their tables created and defined using EF migrations:
[Table("Account")]
public class AccountEntity
{
[Key]
public virtual int Id { get; set; }
}
[Table("Request")]
public class RequestEntity
{
[Key]
public virtual int Id { get; set; }
public int? AccountID { get; set; }
[ForeignKey("AccountID")]
public virtual AccountEntity Account { get; set; }
}
In the Request table, this properly created the FK FK_dbo.Request_dbo.Account_AccountID
Using SSMS, I can confirm the FK is setup properly.
In order to be able to access the Request's one-to-many property from the Account entity, I added the following property to the AccountEntity class:
public virtual ICollection<RequestEntity> Requests { get; set; }
However, now EF suspects that I need to run migrations due to domain changes.
Here's the migration class that it creates, and wants to run:
public partial class RequestUpdate : DbMigration
{
public override void Up()
{
AddColumn("dbo.Request", "AccountEntity_Id", c => c.Int());
CreateIndex("dbo.Request", "AccountEntity_Id");
AddForeignKey("dbo.Request", "AccountEntity_Id", "dbo.Account", "Id");
}
public override void Down()
{
DropForeignKey("dbo.Request", "AccountEntity_Id", "dbo.RealtorAccount");
DropIndex("dbo.Request", new[] { "AccountEntity_Id" });
DropColumn("dbo.Request", "AccountEntity_Id");
}
}
As you can see, EF seems to not recongize/respect that the FK relationshp has already been setup.
I don't suspect that any migrations need to be setup. The FK is already established, and I'm simply adding the collection "navigation" property.
Migrations need to be enabled for this project. EF version is 6, .NET 4.5.
One possible way this can happen is if you have used fluent configuration like this:
modelBuilder.Entity<RequestEntity>()
.HasOptional(e => e.Account)
.WithMany();
and forgot to update the .WithMany() to .WithMany(e => e.Requests) after introducing the collection navigation property, in which case EF considers two one-to-many relationships, hence adds a second FK column with default name.

Discover all DbContext

I have two DbContext in my application, which configure some models with Fluent API. One model of my first DbContext has a foreign key to a second model configured in my second DbContext.
public class UserData
{
public double Id { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public string FullName {
get {
return $"{this.FirstName} {this.LastName}";
}
}
public string Adress1 { get; set; }
public virtual BaseUserTreeData BaseUserTree { get; set; }
public double? BaseUserTreeId { get; set; }
public virtual List<DeviceData> Devices { get; set; }
}
The model BaseUserTreeData is my foreign property configured in my second DbContext.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<BaseUserTreeData>().ToTable("sw_data_baseusertree");
modelBuilder.Entity<BaseUserTreeData>().Property(baseusertree => baseusertree.Id).HasColumnName("baseusertree_ID");
modelBuilder.Entity<BaseUserTreeData>().Property(baseusertree => baseusertree.Label).HasColumnName("label");
modelBuilder.Entity<BaseUserTreeData>().Property(baseusertree => baseusertree.ParentTreeId).HasColumnName("baseUserTree_ID_parent");
modelBuilder.Entity<BaseUserTreeData>().HasKey(baseusertree => baseusertree.Id);
modelBuilder.Entity<BaseUserTreeData>()
.HasOptional(tree => tree.ParentTree)
.WithMany(tree => tree.ChildTrees)
.HasForeignKey(tree => tree.ParentTreeId);
}
When I use UserData with my UserDbContext for the first time, the OnModelCreating of my second DbContext is not called, so BaseUserTreeData mapping is not executed, and the query generated by Entity Framework 6 is wrong. I saw that I can share EntityConfiguration in separate classes, but is there a way to tell to EF6 to call every OnModelCreating of all my DbContext?
I think you're in wrong path.This is not a recommended way of handling Context.If there is too strong relationship between models, you have to concentrate the models inside one unique context.Otherwise you'll have to face so many issues in the future.So my advice is to use the pattern which EF team suggested below.
This is the way Microsoft EF Team has suggested :
When working with Web applications, use a context instance per
request.
You can read more about context handling using below articles :
Working with DbContext
Managing DbContext the right way with Entity Framework

Entity framework relationship broken

I have two models. ApplicationUser:
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public DateTime AccountCreationDate { get; set; }
public virtual ICollection<ProfileView> ProfilesViewed { get; set; }
}
And ProfileView:
public class ProfileView
{
public int Id { get; set; }
public DateTime ViewDate { get; set; }
public virtual ApplicationUser Viewer { get; set; }
public virtual ApplicationUser Viewee { get; set; }
}
Entity framework seems to have created my tables correctly. I can do the following and retrieve a user's ProfileViews:
db.ProfileViews.Where(p => p.Viewer.Id == currentUser.Id);
My problem is that I can't seem to do the following:
db.Users
.Where(u => u.Id == currentUser.Id)
.Include(u => u.ProfilesViewed);
The above returns null for that user, even though it is a Viewer and a Viewee on several ProfileView.
I ran a foreach on all my users, none of them seem to have any ProfilesViewed if I query them from the Users table with Include. I can only retrieve ProfileViews from the ProfileViews table...
Anyone has any idea how to fix this?
Since you did not mention how the ProfileView.Viewer is related to the ApplicationUser.ProfileViewed EF thinks that they are not related(if you check your DB you can see another FK created in the ProfileView for the ApplicationUser.ProfileViewed collection). So adding instances to ProfileView does not effect the User.ProfilesViewed.
Add this code to the Context class, to specify that each ApplicationUser is related to many ProfileView through ProfilesViewed collection.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<ProfileView>().HasRequired(x => x.Viewer)
.WithMany(x => x.ProfilesViewed);
}
The relationships probably need to be explained to be created correctly. I think it's wrong because you have two relationships from ProfileView to ApplicationUser. See the section "Configuring Unconventional Foreign Key Names" in this MSDN article for details of how to configure unconventional relationships using EF Code First.

Categories

Resources