Creating many-to-many relationship on self object class - c#

I currently have a problem for a project where I need to create many-to-many relationships on the same class. I have a class named "Company" where I have some attributes. The relationship I want to create is:
One or more than one company can have one or more competitor. Where a competitor is in fact a company.
I already tried many solutions found on Internet but it seems not to be a good idea to represent the entity relationship I want to have and an easy way to use it in my model.
Here is my class:
public class Company
{
public int Id { get; set; }
public string CompanyName { get; set; }
public string CommercialName { get; set; }
...
}
I tried to add in this class the following line:
public ICollection<Company> Concurrents { get; set; }
But EF code first add a new column "company_id" in my "company" table.
What's the best way to add a list of concurrent in this class that is represented by a new table in my DB with 2 column representing the "Id" of company objects?

Related

How to make one table to many class in EDMX design

This my main class
public partial class MainAcc
{
public int Id { get; set; }
public string Type1 { get; set; }
public string Type2 { get; set; }
public string Type3 { get; set; }
}
and in EDMX design i make new entity base type MainAcc, i delete some column that i want to move in EDMX design, so it become like this after i save
public partial class MainAcc
{
public int Id { get; set; }
public string Type1 { get; set; }
}
public partial class ChildAcc : MainAcc
{
public string Type2 { get; set; }
public string Type3 { get; set; }
}
In design my ChildAcc entity use table map of MainAcc. and after all i got error like this:
Error 3032: Problem in mapping fragments starting at lines 2877, 2907:EntityTypes MyModel.MainAcc, MyModel.ChildAcc are being mapped to the same rows in table MainAcc. Mapping conditions can be used to distinguish the rows that these types are mapped to.
That error in text editor is in MainAcc.
Looks like you can't do this. I think these should be two different tables, but they can be related by some foreign key.
I tried Database First and Model First and the script I ended up adding showed that this would be two different tables related to each other.
Perhaps you expect to have one-to-one or one-to-many relationships, but that doesn't happen within a single table. If you go the Database First way, changing the model may result in an inconsistency with the current context. If the Model First method is adopted, two tables will be automatically generated after the model relationship is established.
I also tried to use table splitting for table mapping, but there are some limitations for dependent entity types, and derived types cannot be mapped to the same table.
Helpful Links:
Advanced table mapping
Model First
Configure One-to-Many Relationships in EF 6
This is just my opinion, hope this helps you. Please help me correct if my understanding is wrong.

Dynamic Entity Navigation Property

I have an entity called Asset, similar to below:
public class Asset
{
public int Id { get; set; }
public int TypeId { get; set; }
public int AddedById { get; set; }
public DateTime DateTimeAdded { get; set; }
public virtual AssetType Type { get; set; }
public virtual ITUser AddedBy { get; set; }
}
I want to be able to have a navigation property that is linked to a single table, but that table is dependent on what type of Asset it is. For instance, if the Asset is of the type "Printer" then I want the navigation property to link to the PrinterDetail entity. My initial way of going about this was to have unused columns in the Asset entity, but I figured that was wasteful or bad practice. Is there something that I am overlooking or is this just something that cannot be done?
Thanks for any advice given.
if you want navigate printerDetail by type you can use entityfraemwork inheritance strategy:
Table per Hierarchy (TPH)
Table per Type (TPT)
Table per Concrete class (TPC)
you have to create Model per each type and use TPT strategy for that.
and then you can use fluent api for config mapping for that.
parent Model (Asset) must define as abstract class and AssesTypes Must be Drive from the Parent.
more information

How to define references when one poco/table has a composite primary key

Considering the documentation here, you can define foreign key relationships in your pocos like the given example:
public class Customer
{
[References(typeof(CustomerAddress))]
public int PrimaryAddressId { get; set; }
[Reference]
public CustomerAddress PrimaryAddress { get; set; }
}
However, let's say that my CustomerAddress poco class actually has to be defined like this because someone decided to design the table like this a long, long time ago.
public class CustomerAddress
{
[PrimaryKey]
public int Id_1 { get; set; }
[PrimaryKey]
public string Id_2 { get; set; }
}
How can I properly define my [Reference] for the PrimaryAddress property in the Customer class with the composite key defined in CustomerAddress?
You can't using APIs that rely on it (but you can still use SELECT)
Please see OrmLite limitations.
I had same problem with a legacy database I can't modify (because another project cohabit with mine).
So I deleted primary key then created a new Id field (autoincrement) on the database (PK) and finally created a unique constraint (NOT NULLABLE) on both fields.
So now, I can use OrmLite to select properly without breaking compatibility with the other project.

NHibernate - Add Comment Property to Entities (Store with Join)

I have a system where I need to be able to add a Comment field onto Customer and Location models but I cannot touch the schema of the existing tables. However, I can add a Comments table. I have simplified this example. We would like the ability to add this Comment to more models moving forward they all use a Guid as Id.
This existing system is a 3rd party system with its own data access layer.
We are just starting to get into NHibernate. From what I can tell it looks like a Join map.
Example:
public class Customer
{
public Guid Id { get; private set; }
public string FirstName { get; private set; }
public string LastName { get; private set; }
public string Comment { get; set; }
}
public class Location
{
public Guid Id { get; private set; }
public string Name { get; private set; }
public string Address { get; private set; }
public string Comment { get; set; }
}
Note: we are sure we want the Comment as a 1-to-1 relationship and not a 1-to-many.
How do I configure a separate table just capture Id and Comment? I'm looking for the right terminology to use. I'm looking for examples with XML (and if possible Fluent config). I would like to keep the Comments for all objects in one table. Thanks.
If you can add Comment table (and corresponding key columns in the existing tables) than fluent mapping can look like
public class CustomerMap : ClassMap<Customer>{
public CustomerMap(){
//...other columns mappings
References(c=>c.Comment).Column("CommentId");
}
}
And repeat it for other entities as well. You can set desired fetch-mode(join) and other action there as well. I have wrote References there (so many-to-one) but if you need one-to-one mapping it is not a big difference
If you can't change the database schema your options are very limited.
MAYBE, you can do it using the mapping.
Take a look here:
http://ayende.com/blog/3961/nhibernate-mapping-join
Try to use the same column name in mapping for all entities.

EF 4.3 Code First one-to-one relationship when using an existing database

I have two tables in my database which have a one-to-one relationship. I want to access them with EF using the code first approach. I have written the POCO classes like this:
public class User
{
public int Id { get; set; }
public string Login { get; set; }
public string Password { get; set; }
public string Email { get; set; }
public virtual Profile Profile { get; set; }
}
public class Profile
{
public int UserId { get; set; }
public string FullName { get; set; }
public DateTime? Birthday { get; set; }
public bool? Male { get; set; }
}
And also I described the relationships between that classes in the OnModelCreating as explained here. So that's how looks:
modelBuilder.Entity<Profile>().HasKey(x => x.UserId);
modelBuilder.Entity<User>().
HasOptional(u => u.Profile).
WithRequired();
But when I run the application I'm getting this exception when trying to access the User tables:
Invalid column name "User_Id"
Although when I let EF create a new database on its own with these two tables, it creates a database that has the same structure as mine and it works.
I was using EF 4.3 (I tried 4.2 too) and MS SQL 2008. What am I doing wrong? I can't believe that it's a bug of Entity Framework.
EF requires that one-to-one columns both share the same primary key, and that there is a referential FK between the two.
If this is the case, you can use the Entity Framework Power Tools CTP1 to reverse engineer your database as a Code First model.
You can find it here: http://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d
Thank you, guys, for your help. I realized that code first is not the way I should go and I have chosen the database first approach. With the POCO templates for the Entity Framework.

Categories