last day (14 days later) » 

7:29 AM
1
A: Entity Framework Code First - map same entity twice for different purpose

SBirthareThe problem you are observing is called "Multiple Cascade Path". A Multiple Cascade Path happens when a cascade path goes from column col1 in table A to table B and also from column col2 in table A to table B. The exception is caused by SQL Server when code first attempted to add table that has ...

 
Now it causes another error.Quotation_CancelledUser_Target_Quotation_CancelledUser_Source: : The types of all properties in the Dependent Role of a referential constraint must be the same as the corresponding property types in the Principal Role. The type of property 'CancelledUserID' on entity 'Quotation' does not match the type of property 'Id' on entity 'ApplicationUser' in the referential constraint 'Quotation_CancelledUser'. Quotation_CreatedUser_Target_Quotation_CreatedUser_Source: :
The types of all properties in the Dependent Role of a referential constraint must be the same as the corresponding property types in the Principal Role. The type of property 'CreatedUserID' on entity 'Quotation' does not match the type of property 'Id' on entity 'ApplicationUser' in the referential constraint 'Quotation_CreatedUser'.
 
Use .HasForeignKey(u => u.CreatedUserID).WillCascadeOnDelete(false);
@Athul - EF Version? I am using EF5 and the above code is working fine.
 
I am on EF 6, Maybe that's why the error
Tried . same error
 
Athul, I just checked. The above code works with EF6 as well with no changes. I think you are getting error due to incorrect key configuration in your set up.
What's primary key in your ApplicationUser class?
 
I am using ASP.Net Identity and ApplicationUser inherits key and all from IdentityUser class
public class ApplicationUser : IdentityUser
{
[Required, EmailAddress, StringLength(200)]
public string Email { get; set; }

}
 
7:33 AM
What's key column for this table in DB?
 
Full script of table
CREATE TABLE [dbo].[IdentityUser] (
[Id] NVARCHAR (128) NOT NULL,
[UserName] NVARCHAR (MAX) NULL,
[PasswordHash] NVARCHAR (MAX) NULL,
[SecurityStamp] NVARCHAR (MAX) NULL,
[Email] NVARCHAR (200) NULL,
[FirstNsme] NVARCHAR (50) NULL,
[LastName] NVARCHAR (MAX) NULL,
[Mobile] NVARCHAR (32) NULL,
[HomePhone] NVARCHAR (32) NULL,
[WorkPhone] NVARCHAR (32) NULL,
[DepartmentID] INT NULL,
[Discriminator] NVARCHAR (128) NOT NULL,
 
NVARCHAR hmmm
 
yes that must be the reason
 
The type of property 'CancelledUserID' on entity 'Quotation' does not match the type of property 'Id' on entity 'ApplicationUser'.
 
any work around to solve this isue?
 
7:40 AM
Try changing int to string in Quotation class.
 
k
Yes that solved the error
But foreign key mapping is set to only createduser now:(
CREATE TABLE [dbo].[Quotation] (
[QuotationID] INT IDENTITY (1, 1) NOT NULL,
[QuotationCode] NVARCHAR (MAX) NULL,
[QuotationDate] DATETIME NOT NULL,
[QuotationCancelDate] DATETIME NOT NULL,
[CreatedUserID] NVARCHAR (128) NOT NULL,
[CancelledUserID] NVARCHAR (MAX) NULL,
[QuotationCancellationReason] NVARCHAR (MAX) NULL,
CONSTRAINT [PK_dbo.Quotation] PRIMARY KEY CLUSTERED ([QuotationID] ASC),
CONSTRAINT [FK_dbo.Quotation_dbo.IdentityUser_CreatedUserID] FOREIGN KEY ([CreatedUserID]) REFERENCES
 
There you go...
 
any guess?
 
Ah that should be again due to some configuration. I could see both key setup in my db.
Hey did you added WillCascadeOnDelete()?
Try making both entity configuration exactly same.
 
 
3 hours later…
10:43 AM
Yes. That worked
thanks for your help and time
:)
 

last day (14 days later) »