Skip to main content
added 30 characters in body
Source Link
marc_s
  • 759.9k
  • 186
  • 1.4k
  • 1.5k

It seems to me like the ForeignKeyForeignKey attribute is not working for me, but I guess I'm using it wrong;wrong ;). It's

It's easier to explain with code:

 public class BaseCard  { public int Id {get ; set; } public int BaseCardId { get; set; } public List<Skill> Skills { get; set; }  }  public class Skill  { public int Id { get; set; } public int BaseCardId { get; set; } [ForeignKey("BaseCardId")] public BaseCard BaseCard { get; set; }  } 

When I try to fill these objects with the seed method, I'm getting this error: INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Skills_dbo.BaseCards_BaseCardId". The conflict occurred in database "Database", table "dbo.BaseCards", column 'Id'.

INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Skills_dbo.BaseCards_BaseCardId". The conflict occurred in database "Database", table "dbo.BaseCards", column 'Id'.

It seems to me like the ForeignKeyForeignKey in SkillSkill tries to point at the IdId column of BaseCardsBaseCards instead of the BaseCardIdBaseCardId column, and I can't figure out why..

If I try to remove the "normal" IdId property of BaseCardBaseCard, and set the BaseCardIdBaseCardId as the PK (with attribute [Key][Key]), I get the following error: Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.

Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.

Does anyone know how I can get this code to work so the property BaseCardIdBaseCardId from the SkillSkill class will point to the BaseCardIdBaseCardId property of BaseCardBaseCard, instead of apparently the IdId property?

Thanks in advance!

It seems to me like the ForeignKey attribute is not working for me, but I guess I'm using it wrong;). It's easier to explain with code:

 public class BaseCard  { public int Id {get ; set; } public int BaseCardId { get; set; } public List<Skill> Skills { get; set; }  }  public class Skill  { public int Id { get; set; } public int BaseCardId { get; set; } [ForeignKey("BaseCardId")] public BaseCard BaseCard { get; set; }  } 

When I try to fill these objects with the seed method, I'm getting this error: INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Skills_dbo.BaseCards_BaseCardId". The conflict occurred in database "Database", table "dbo.BaseCards", column 'Id'.

It seems to me like the ForeignKey in Skill tries to point at the Id column of BaseCards instead of the BaseCardId column, and I can't figure out why..

If I try to remove the "normal" Id property of BaseCard, and set the BaseCardId as the PK (with attribute [Key], I get the following error: Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.

Does anyone know how I can get this code to work so the property BaseCardId from the Skill class will point to the BaseCardId property of BaseCard, instead of apparently the Id property?

Thanks in advance!

It seems to me like the ForeignKey attribute is not working for me, but I guess I'm using it wrong ;)

It's easier to explain with code:

public class BaseCard { public int Id {get ; set; } public int BaseCardId { get; set; } public List<Skill> Skills { get; set; } } public class Skill { public int Id { get; set; } public int BaseCardId { get; set; } [ForeignKey("BaseCardId")] public BaseCard BaseCard { get; set; } } 

When I try to fill these objects with the seed method, I'm getting this error:

INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Skills_dbo.BaseCards_BaseCardId". The conflict occurred in database "Database", table "dbo.BaseCards", column 'Id'.

It seems to me like the ForeignKey in Skill tries to point at the Id column of BaseCards instead of the BaseCardId column, and I can't figure out why..

If I try to remove the "normal" Id property of BaseCard, and set the BaseCardId as the PK (with attribute [Key]), I get the following error:

Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.

Does anyone know how I can get this code to work so the property BaseCardId from the Skill class will point to the BaseCardId property of BaseCard, instead of apparently the Id property?

Thanks in advance!

spelling
Source Link
dbugger
  • 16.6k
  • 10
  • 34
  • 36

It seems to me like the ForeignKey attribute is not working for me, but I guess I'm using it wrong;). It's easyereasier to explain with code:

 public class BaseCard { public int Id {get ; set; } public int BaseCardId { get; set; } public List<Skill> Skills { get; set; } } public class Skill { public int Id { get; set; } public int BaseCardId { get; set; } [ForeignKey("BaseCardId")] public BaseCard BaseCard { get; set; } } 

When I try to fill these objects with the seed method, I'm getting this error: INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Skills_dbo.BaseCards_BaseCardId". The conflict occurred in database "Database", table "dbo.BaseCards", column 'Id'.

It seems to me like the ForeignKey in Skill tries to point at the Id column of BaseCards instead of the BaseCardId column, and I can't figure out why..

If I try to remove the "normal" Id property of BaseCard, and set the BaseCardId as the PK (with attribute [Key], I get the following error: Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.

Does anyone know how I can get this code to work so the property BaseCardId from the Skill class will point to the BaseCardId property of BaseCard, instead of apperentlyapparently the Id property?

Thanks in advance!

It seems to me like the ForeignKey attribute is not working for me, but I guess I'm using it wrong;). It's easyer to explain with code:

 public class BaseCard { public int Id {get ; set; } public int BaseCardId { get; set; } public List<Skill> Skills { get; set; } } public class Skill { public int Id { get; set; } public int BaseCardId { get; set; } [ForeignKey("BaseCardId")] public BaseCard BaseCard { get; set; } } 

When I try to fill these objects with the seed method, I'm getting this error: INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Skills_dbo.BaseCards_BaseCardId". The conflict occurred in database "Database", table "dbo.BaseCards", column 'Id'.

It seems to me like the ForeignKey in Skill tries to point at the Id column of BaseCards instead of the BaseCardId column, and I can't figure out why..

If I try to remove the "normal" Id property of BaseCard, and set the BaseCardId as the PK (with attribute [Key], I get the following error: Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.

Does anyone know how I can get this code to work so the property BaseCardId from the Skill class will point to the BaseCardId property of BaseCard, instead of apperently the Id property?

Thanks in advance!

It seems to me like the ForeignKey attribute is not working for me, but I guess I'm using it wrong;). It's easier to explain with code:

 public class BaseCard { public int Id {get ; set; } public int BaseCardId { get; set; } public List<Skill> Skills { get; set; } } public class Skill { public int Id { get; set; } public int BaseCardId { get; set; } [ForeignKey("BaseCardId")] public BaseCard BaseCard { get; set; } } 

When I try to fill these objects with the seed method, I'm getting this error: INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Skills_dbo.BaseCards_BaseCardId". The conflict occurred in database "Database", table "dbo.BaseCards", column 'Id'.

It seems to me like the ForeignKey in Skill tries to point at the Id column of BaseCards instead of the BaseCardId column, and I can't figure out why..

If I try to remove the "normal" Id property of BaseCard, and set the BaseCardId as the PK (with attribute [Key], I get the following error: Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.

Does anyone know how I can get this code to work so the property BaseCardId from the Skill class will point to the BaseCardId property of BaseCard, instead of apparently the Id property?

Thanks in advance!

Source Link
Rob
  • 151
  • 2
  • 4
  • 13

Entity Framework Code First Foreign Key issue

It seems to me like the ForeignKey attribute is not working for me, but I guess I'm using it wrong;). It's easyer to explain with code:

 public class BaseCard { public int Id {get ; set; } public int BaseCardId { get; set; } public List<Skill> Skills { get; set; } } public class Skill { public int Id { get; set; } public int BaseCardId { get; set; } [ForeignKey("BaseCardId")] public BaseCard BaseCard { get; set; } } 

When I try to fill these objects with the seed method, I'm getting this error: INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Skills_dbo.BaseCards_BaseCardId". The conflict occurred in database "Database", table "dbo.BaseCards", column 'Id'.

It seems to me like the ForeignKey in Skill tries to point at the Id column of BaseCards instead of the BaseCardId column, and I can't figure out why..

If I try to remove the "normal" Id property of BaseCard, and set the BaseCardId as the PK (with attribute [Key], I get the following error: Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.

Does anyone know how I can get this code to work so the property BaseCardId from the Skill class will point to the BaseCardId property of BaseCard, instead of apperently the Id property?

Thanks in advance!