Naming collision with EntityFramework - c#

I am using EF to access a SQL DB. For the sake of this example Lets assume I have a Users Table, a User Comments Table (comments made by users) and a Liked comments table (comments a user marked like on them) when the Context is generated I get in the User.cs file the following ICollections:
public virtual ICollection<Comment> Comments { get; set; }
public virtual ICollection<Comment> Comments1 { get; set; }
How can I know which is which? Why does EF not add the foreign key table name to the column?
I have several of these issues.

in the edmx designer, click on the navigation property ("Comments" or "Comments1") and press F4 to show it's properties. The properties panel will display the foreign key name which will give you enough info to identify which is which.

Related

Entity Framework: how to add an association, based on an existing property

I'm using the database-first approach while setting my first steps in Entity Framework. In my SQL Server database I have two tables, Location and Area.
Both have an Id column and the Location table already has an AreadId property. I would like to use this property for creating a one-to-many association between both tables (many Location entries for one Area entry), so I have created the following in my EDMX diagram using the EDMX-designer:
In the automatically generated Location.cs file, it looks as follows:
public partial class Location
{
...
public int AreaId { get; set; }
...
public virtual Area Area { get; set; }
}
I would like to use the already existing property Location.AreaId as the foreign key. How can I do that, using EDMX-designer?
Please keep in mind that I can modify the EDMX diagram, but not the automatically generated Location.cs file.
Thanks in advance

have primary key set up but says it is not defined?

before you link to another post, i am using migrations and all that i can find don't use the way i have to do it.
first this is homework,
second here is a link to a google drive with the "full" project project fill stuff that was given to help, and a word doc with specifications(but the last one is not as important)
so on to the problem i am tring to do my homework the package manager says this "The entity type 'Categories' requires a primary key to be defined. If you intended to use a keyless entity type call 'HasNoKey()'." almost no matter what i do. i can get it to stop but then the database is not actual made at all.
my teacher is not responing to my emails and the other students in my class that have responed have the same issue, and this is the very beginning of the project and for the last few days i have been stuck here so please help.
i will add edit as questions get asked, i am panicking on getting this done in time so i don't know what is important or not.
First of all, double check you actually set PrimaryKey on your Database table. Then use [Key] attribute from System.ComponentModel.DataAnnotations namespace.
public class Categories
{
[Key]
public string CategoryId { get; set; }
public string Name { get; set; }
}
And also, next time, make sure your ID fields are either numeric or Guid and auto generated by database identity specification.. string Ids are slow for querying db records and to index tables.
Must tell model CategoryId is key. Best to use int as type.
[Key]
public int CategoryId { get; set; }

Entity framework navigation properties naming convention

I have two tables named as Profile and ProfileHistory.
Each record in ProfileHistory has to belong to a profile in Profile table, so there is a foreign key relation between two tables. Besides, in ProfileHistory table, there is a column named as ManagerId which also relates to Profile table with foreign key relation.
Profile table structure
Id int primary key
....
....
ProfileHistory table structure
Id int primary key
ProfileId int foreign key to Profile table
ManagerId int foreign key to Profile table
....
My question is:
Since currently I only know this, I am creating my entity model from database.
Model and therefore entity classes are created with navigation properties in
ProfileHistory entity like following:
public virtual Profile Profile { get; set; }
public virtual Profile Profile1 { get; set; }
It is so confusing. Because it is not clear which navigation property for which relation.
Even it is worse if I have more relations between two tables. navigation property names are becoming Profile, Profile1, Profile2, etc.
I was expecting to have the name of the navigation properties related with its foreign key relations.
How can I make my navigation property names something that related to its foreign key relation, in my case "from Profile1 to ProfileManager" ?
Thank in advance for your kind helps.
Muharrem
You can always rename the properties in model diagram. The name can be found in Properties window when you click on a navigation property.
I haven't tested it, but you can map a property to a column using an attribute:
[Column(“BlogDescription", TypeName="ntext")]
public virtual Profile Profile { get; set; }
[Column("Profile1", TypeName="int")]
public virtual Profile ProfileManager { get; set; }
Change the type and the name of the column as it is in the database.
The way I usually solve this is to add properties through partial classes that better represent what I'm after. This way if I need to delete the entity from the diagram and re-add it, I don't lose any renamed columns from the model.
The downside to this is that you need to remember that you cannot use them in Queries because EF won't know how to translate it into a SQL query. But if you've already got your Profile object, it's a lot easier to access myProfile.Manager than myProfile.Profile1.
So, for example, if EF created this for you:
public partial class ProfileHistory
{
public virtual Profile Profile { get; set; }
public virtual Profile Profile1 { get; set; }
}
I would end up creating a partial class like this to re-map the columns:
public partial class ProfileHistory
{
public Profile Manager
{
get
{
return this.Profile1;
}
set
{
this.Profile1 = value;
}
}
}
I did face the same problem some time ago. Well, it is even bigger then just confusing names. If you have navigation properties to another table, like Profile, Profile1, Profile2, next you delete/edit the corresponding foreign keys you may end up having those mixed. And if you used EntitySQL to query data you'll end up having bugs because of incorrect data retrieved/wrong table join conditions...
What I did was changing the t4 template and modified the way properties are generated. When property code text is being written you have the information about association and foreign key related to it. Foreign key names are unique in database and I named those with following pattern
FK_[Table]_[Meaning]
...
FK_ProfileHistory_InitialProfile
FK_ProfileHistory_UpdatedProfile
Next, having this information, I named the properties with the [Meaning] part of the foreign key name.

Choosing name of column in entity framework code-first migrations

I have seen similar questions to this; but none quite the same; and none have helped me. I want the migration to use a different name for the column than I have for the property in my class. On built-in types, I am able to do this with [Column("newName")]. However, this doesn't work when I want a foreign key to another class.
In other words, this works just fine:
[Column("NameInDB")]
public string NameInCode { get; set; }
But this doesn't work at all:
[Column("Employee_Id")]
public virtual Employee Owner { get; set; }
In the second case, the migration still creates the column as Owner_Id; it completely ignores the Column annotation. Is there somewhere that it says that the Column annotation only works for built-in types? I couldn't find anything about that.
I know that it is possible to use the [ForeignKey] annotation to do this, but if I do, I have to have an extra property in my code that I don't want:
[ForeignKey("Employee_Id")]
public virtual Employee Owner { get; set; }
public int Employee_Id { get; set; }
I don't want to do that because the Employee_Id property is redundant in that case; I'd rather just use the Owner property. Is there a way around this, or a good reason that [Column] seems to be ignored?
public virtual Employee Owner { get; set; } is not mapped to DB, it is not represented in database. It populated by Entity Framework automatically for your convenience. Employee_Id - this is what is stored in DB. Entity Framework uses Employee_Id field to create Owner object. So, Emplyee_Id is not redundant in that case, it is mandatory and it has physical representation in DB, but Owner field is logical part of the class, managed by Entity Framework and not have it's column in DB. Makes sense?

How to load navigation data in Entity framework Code first?

I have the following situation:
http://www.mediafire.com/view/?brjyqlj4rvjako9
When you read the picture in your browser you can see I have a three tables. The middle one is junction table.
All works well except the field Room in middle table (Bookings table) that always has a NULL value:
http://www.mediafire.com/view/?axz7ljskgcpl8cp
So, I saw that it needs to be loaded by hand before I make a reading operation on it.
How is this done in Entity Framework Code First as my project is running on it?
The relationship is many to many between guest and Room, Booking is a junction table.
Thank you
Is the property virtual?:-
public class Booking
{
...
public int RoomId { get; set; }
public virtual Room Room { get; set; } // Needs to be virtual
...
}
Navigation properties need to be virtual in order to enable lazy-loading.
If that's not the problem then I'll need you to post the source for your three entities (Room, Guest, Booking)

Categories

Resources