Multiple relationships between two models - c#

I am using code first and EF 4.1 im my MVC3 App and have two models that have two
relationships between them. Here is the classes that refers to these models:
public class Cattle
{
public int CattleID { get; set; }
public int FarmID { get; set; }
public int SituationID { get; set; }
public int DiscardID { get; set; }
public int Stamp { get; set; }
public string Sex { get; set; }
public string Condition { get; set; }
public virtual Farm Farm { get; set; }
[InverseProperty("Cattles2")]
public virtual ICollection<Farm> Farms { get; set; }
}
public class Farm
{
public int FarmID { get; set; }
public string Name { get; set; }
public virtual ICollection<Cattle> Cattles { get; set; }
public virtual ICollection<Cattle> Cattles2 { get; set; }
}
One relationship is one to one or many cause a catle can only be in a farm and a farm can contains many catles. Another relationship is many to many that a catle can be transferred between farms and i would generate a third table to store the transfers using Fluent API. I also would like to insert a Date property to the new table and donĀ“t know how to do it.Here is the code in the FarmContext inside the OnModelCreating method:
modelBuilder.Entity<Catle>()
.HasMany(c => c.Farms).WithMany(i => i.Catles)
.Map(t => t.MapLeftKey("CatleID")
.MapRightKey("FarmID")
.ToTable("CatleTransfer"));
When i build the project it seems to work fine and the generated tables Catle and Farm are fed by the FarmInitializer but some information that points to this relationship is missing in the index page of Catle. its appearing in blank to me. Here is the code that gets the information:
#Html.DisplayFor(modelItem => item.Farm.Name)
I need Knowing what i am doing wrong or if there is a more appropriate method to resolve that issue.

You may need to be specific about the relationship when dealing with multiple joins.
Try adding a Foreign Key attribute to your Farm navigation property.
[ForeignKey("FarmID")]
public virtual Farm Farm { get; set; }

Related

EF Core - Shared Primary Key Associations via Fluent API

I have an 'User'class contains two 'Address' properties reference to 'Address' entity, and there is another class - 'Shipment' also associate with 'Address'.
How i can use fluent api on ef core to build a correct relation between entities.
public class Address
{
public int AddressId { get; set; }
public string Street { get; set; }
...
}
public class User
{
public int UserId { get; set; }
public string Name { get; set; }
public virtual Address DefaultAddress { get; set; }
public virtual Address BillingAddress { get; set; }
}
public class Shipment
{
public int ShipmentId { get; set; }
public virtual Address DeliveryAddress { get; set; }
}
There is not need to declare explicit configuration, the EF will do everything without any help.
I prepared a working example with and without fluent configuration, you can check it out here. Just switch between commits to see the difference.
As you can notice, there is no differences in generated migration.

double relationship between entities in EntityFramework 6

My problem looks simple. I need to implement a relationships between items in the database. For example: relationship between entities like computer and software shows users that computer stores a specific software and similarly - a software is installed in the specific computer. I think I should implement an entity with source id and target id or something similar. I wrote some code using code first in EntityFramework 6. Here are two classes:
public class ConfigurationItem
{
public int Id { get; set; }
public String Name { get; set; }
public String DeploymentState { get; set; }
public String IncidentState { get; set; }
[DataType(DataType.MultilineText)]
public String Description { get; set; }
[DataType(DataType.MultilineText)]
public String Note { get; set; }
public virtual ICollection<Relationship> Relationship { get; set; }
}
public class Relationship
{
[Key]
public int RelationshipId { get; set; }
[ForeignKey("ConfigurationItem")]
public int SourceId { get; set; }
[ForeignKey("ConfigurationItem")]
public int TargetId { get; set; }
public String Type { get; set; }
public virtual ConfigurationItem Source { get; set; }
public virtual ConfigurationItem Target { get; set; }
}
This solution doesn't work. I need a tip or something what should I try to make it work properly. EF throws an error about foreign key:
The ForeignKeyAttribute on property 'SourceId' on type 'cms_1.Models.Relationship' is not valid. The navigation property 'ConfigurationItem' was not found on the dependent type 'cms_1.Models.Relationship'. The Name value should be a valid navigation property name.
When I try to resolve it EF throws an error about cascade deleting. I know how to disable it but I just don't want to. I need a proper solution with that feature but I think I don't know how to do a model representing given scenario.
Simply - I need to store two foreign keys from entity "A" in the entity "B". How is it possible?
from a quick review , I can tell that you need 3 tables :
first : Computer
second : Software
third : a table , lets call it ComputerSoftware which tell which software has in what computer ( or you can also see it - which computer use what software ), which has ComputerID column and SoftwareID column.
example (source)
class Country
{
public int Id { get; set; }
public virtual ICollection<CountryCurrency> CountryCurrencies { get; set; }
}
class Currency
{
public int Id { get; set; }
}
class CountryCurrency
{
[Key, Column(Order=0)]
public virtual int CountryId { get; set; }
[Key, Column(Order=1)]
public virtual int CurrencyId { get; set; }
public virtual Country Country { get; set; }
public virtual Currency Currency { get; set; }
}
Your issue could be that in the migration file creating those tables, it will have something like
.ForeignKey("dbo.Relationship", t => t.Id, cascadeDelete: true)
This will be set on both tables, ConfigurationItem and Relationship of their Primary Key fields. When you delete one, that config tells SQL Server to delete the relationships as well and the relationship probably has a cascadeDelete: true to the parent. This will cause the cyclical cascading delete issue you are experiencing.
After the migration has been generated, go in and change one or all to cascadeDelete: false and this will fix that issue. This is what EF generates by default if I recall.

Creating many to many junction table in Entity Framework

I am trying to add a many to many relationship between two of my entities. I need a junction table with an additional field, I'm aware that means EF cannot do this automatically and that I need to create an Entity for my junction table.
I have the following models
public class Supplier
{
public int Id { get; set;}
public virtual ICollection<SupplierUsers> UserPermissions { get; set; }
}
And
public class User
{
public string Id { get; set;}
public virtual ICollection<SupplierUsers> UserPermissions { get; set; }
}
I need for a user to have a permission stored in the junction table. So I have created the following entity
public class SupplierUsers
{
public string UserId { get; set; }
public int SupplierId { get; set; }
public SupplierUserPermission Permission { get; set; }
public virtual Supplier Supplier { get; set; }
public virtual User User { get; set; }
}
In my OnModelCreating I've also added the following (this is probably where I'm going wrong)
modelBuilder.Entity<SupplierUsers>()
.HasKey(x => new { x.UserId, x.SupplierId });
This works to an extent, I can successfully add a user/supplier/permission to this table.
But I cannot add the same user / supplier multiple times to this table (probably due to the PK?).
How can I alter my code so that I can add the same user or supplier multiple times in this table?
Here's what the table structure looks like at the moment:
Thank you.
If i understand you correctly you want to add multiple equal pairs of UserId and SupplierId to SupplierUsers, right?
Add a SupplierUsersId field to your SupplierUsers entity and make it primary key.
public class SupplierUsers
{
public int SupplierUsersId { get;set; }
public string UserId { get; set; }
public int SupplierId { get; set; }
public SupplierUserPermission Permission { get; set; }
public virtual Supplier Supplier { get; set; }
public virtual User User { get; set; }
}
Remove the configuration from OnModelCreating()

Relationship troubles with Entity Framework

I need help creating the relationship in entity framework as everything I have tried gives me errors when trying to add the migration or if I get passed that then I try to update the database and get an error about indexes with the same name.
public class Profile
{
public Profile()
{
Environments = new HashSet<Environment>();
}
[Key]
public int Id { get; set; }
public string VersionCreated { get; set; }
public string DiskLocation { get; set; }
public string Name { get; set; }
public DateTime DateTime { get; set; }
public virtual Product Product { get; set; }
public virtual Instance OriginalInstance { get; set; }
public virtual ICollection<Environment> Environments { get; set; }
}
public class Instance
{
public Instance()
{
TestResults = new HashSet<TestResult>();
Environments = new HashSet<Environment>();
}
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Version { get; set; }
public string UserFriendlyName { get; set; }
public virtual Product Product { get; set; }
public virtual Profile LastKnownProfile { get; set; }
public virtual Computer Computer { get; set; }
public virtual ICollection<TestResult> TestResults { get; set; }
public virtual ICollection<Environment> Environments { get; set; }
}
The problem with the above classes is that the OrginalInstance property on the Profile class and the LastKnownProfile in the Instance class are supposed to just be foreign keys to those specific tables and they probably won't be the same very often. They can also both possibly be null.
I have tried:
modelBuilder.Entity<Instance>().HasRequired(i => i.LastKnownProfile);
modelBuilder.Entity<Profile>().HasRequired(p => p.OriginalInstance);
This gave me an Unable to determine the principal end of an association between the types 'EcuWeb.Data.Entities.Instance' and 'EcuWeb.Data.Entities.Profile'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations. error.
and with:
modelBuilder.Entity<Instance>().HasRequired(i => i.LastKnownProfile).WithOptional();
modelBuilder.Entity<Profile>().HasRequired(p => p.OriginalInstance).WithOptional();
The database adds a foreign key reference back to itself.
...that the OrginalInstance property on the Profile class and the
LastKnownProfile in the Instance class are supposed to just be foreign
keys to those specific tables and they probably won't be the same very
often. They can also both possibly be null.
In this case you actually want two one-to-many relationships between Profile and Instance if I don't misunderstand your quote above. It would mean that many Profiles can have the same OriginalInstance and that many Instances can have the same LastKnownProfile. The correct mapping would look like this then:
modelBuilder.Entity<Profile>()
.HasOptional(p => p.OriginalInstance)
.WithMany()
.Map(m => m.MapKey("OriginalInstanceId"));
modelBuilder.Entity<Instance>()
.HasOptional(i => i.LastKnownProfile)
.WithMany()
.Map(m => m.MapKey("LastKnownProfileId"));
The lines with MapKey are optional. Without them EF will create a foreign key with a default name.
Also note that you must use HasOptional (instead of HasRequired) if "both can possibly be null".

Fluent NHibernate - Mapping results from stored procedure into multiple models

I'm trying to map the results of a stored procedure into a model that contains another model several times. I'm not sure if this is possible, but wanted to ask and see if this is a valid approach, or if it would be better to define everything out individually.
My model classes look like this:
public class ClientSummary {
public virtual int Id { get; set; }
public virtual int Client_ID { get; set; }
public virtual string Client_Name { get; set; }
public virtual string State { get; set; }
public virtual EDITotal Totals { get; set; }
public virtual EDITotal In_Totals { get; set; }
public virtual EDITotal Out_Totals { get; set; }
public virtual EDITotal Direct_Totals { get; set; }
public virtual EDITotal CPN_Totals { get; set; }
}
public class EDITotal {
public virtual int Count { get; set; }
public virtual double Charges { get; set; }
public virtual double StateSavings { get; set; }
public virtual double PPOSavings { get; set; }
public virtual float PPO_Pct_Bill { get; set; }
public virtual float PPO_Pct_State { get; set; }
public virtual int Unique_TaxIds { get; set; }
}
What I'm not sure of is what my class map would look like in Fluent NHibernate, or if something like this is possible. I'm mainly trying to keep things tidy, and reusable. I'm sure I'll have other reports that will use similar metrics. Am I going about this the right way, or do I really need to define a full model for each report I build? This is all taking place inside a MVC Web application.
Thanks in advance!
Yes this is indeed possible and right or wrong you can get it working with the way you have your classes setup. To do this, you can use a mapper like this:
public class ClientSummaryMappingOverride : IAutoMappingOverride<ClientSummary>
{
public void Override(AutoMapping<ClientSummary> mapping)
{
mapping.References(x => x.Totals, "Totals_id");
mapping.References(x => x.In_Totals, "In_Totals_id");
mapping.References(x => x.Out_Totals, "Out_Totals_id");
mapping.References(x => x.Direct_Totals, "Direct_Totals_id");
mapping.References(x => x.CPN_Totals, "CPN_Totals_id");
}
}
Once you have your map configured, you just need to make sure your Stored Procedure or SQL Query returns the ClientSummary records with the appropriate "Totals_id" type fields. NHibernate will pick up those ids and map them to the correct data (Lazy Load I believe, depending on your conventions or other mappings).

Categories