0

Basically, I have two tables. Principal table let's say "Foo" that has PK column ID of type int, and dependent table "Bar" that has PK column FooID of type int that is also a FK to ID of "Foo".

As I understood, the problem is that dependent type lacks [Foreign Key] attribute on the primary key property. Problem can be resolved on dependent type's side by either specifying [Foreign Key] attribute on PK or specifying [Required] attribute on the navigation property of PK.

I have tried to add [Required] but it didn't help much. Problem was resolved but models couldn't pass validation because of that. I don't want them to be validated because those properties can be null sometimes, at least when I use them to insert to DB. Turning off validation check on SaveChanges() also didn't help because I started to get other weird exceptions.

Now I want to try specifying [ForeignKey] attribute but can't figure out how to make it happen. I don't have experience with T4 (*.tt file). I don't know how to check property if it's a fk and don't know how to get its name. So please help me.

  • It is bad idea: column is Primary Key and Foreign Key at the same time. – Hemid Abbasov Jan 12 '18 at 13:13
  • If you want to implement One-To-One relationship : http://www.tech-recipes.com/rx/56738/one-to-one-one-to-many-table-relationships-in-sql-server/ – Hemid Abbasov Jan 12 '18 at 13:20
  • and may be usefull https://stackoverflow.com/questions/10292355/how-do-i-create-a-real-one-to-one-relationship-in-sql-server – Hemid Abbasov Jan 12 '18 at 13:31
  • Thanks for the help. Why do you think it is a bad idea? Can you elaborate on this? Because that is how you would implement entity inheritance on DB. Anyway I'm not allowed to make changes in the database. I have to use it as it is. – Darkhan Zholmukhanov Jan 12 '18 at 17:23
  • If you look at second link, you will find why it is bad. For One-to-One ( but really it is 1 to 0..1 relationship) relationship you need just UNIQUE key on second table, not PRIMARY. Are you sure that there is PRIMARY key on table "Bar"? Can you show database tables creation scripts? – Hemid Abbasov Jan 12 '18 at 17:37
  • Please, edit your initial post with adding exact error mesaage, what you want to get (class samples), database schema and so on. Because in above link (another question on SO) questioner has the similar tables and gets the models (classes) normal, his question is about different thing. – Hemid Abbasov Jan 12 '18 at 20:14
  • Thanks for the help Hemid! But I can't exactly share the code and schema because I'm not allowed to. I tried you suggestion. Deleted PK and instead used Unique key. Re-created edmx and everything worked just fine. Then I reverted to PK. And it was working as well. As if nothing was wrong. I wrote details on my answer. – Darkhan Zholmukhanov Jan 14 '18 at 05:20

1 Answers1

0

I tried to re-create EDMX and ran some db insertions on a different machine. Strangely, this error does not occur on that machine. Found out recently that EF does not need DataAnnotaions because it uses mappings from XML files not from code from here. So I guess the problem was that my old pc somehow couldn't correctly create EDMX or couldn't use mappings from xml. The cause is not clear to me yet. But the problem was resolved.