0

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?

tereško
  • 58,060
  • 25
  • 98
  • 150
matB
  • 23
  • 2
  • 7
  • Are you using migrations? – Tacoman667 Jul 01 '14 at 18:39
  • Yes, a few minutes ago I found a solution. I wrote two navigation properties in ConfigurationItem, code runs successfuly but I don't know if it is done in a way that I want it to work – matB Jul 01 '14 at 18:45

2 Answers2

0

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; }
}
Community
  • 1
  • 1
Zakos
  • 1,492
  • 2
  • 22
  • 41
  • I would be ok if i had only 2 entities but ConfigurationItem entity is extended by Computer, Software, Location etc. I just want to associate ConfigurationItem entities with each other. – matB Jul 01 '14 at 18:29
  • I think its still possible , just that Aid and Bid are the same entity – Zakos Jul 02 '14 at 06:38
0

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.

Tacoman667
  • 1,391
  • 9
  • 16