问题描述:

I have the following entities:

public abstract class Meter

{

public int MeterId { get; set; }

public string EANNumber { get; set; }

public string MeterNumber { get; set; }

[Required]

public virtual Premise Premise { get; set; }

public abstract void AddReading(CounterReading reading);

}

public class GasMeter : Meter

{

public virtual Counter Counter { get; private set; }

public override void AddReading(CounterReading reading)

{

Counter.Readings.Add(reading);

}

}

public class Premise

{

[Key]

public int PremiseId { get; set; }

public string Title { get; set; }

public string Description { get; set; }

[Required]

public virtual Address Address { get; set; }

public string Type { get; set; }

public virtual GasMeter GasMeter { get; set; }

}

I have a 1:1 relation between a GasMeter and a Premise.

What must I do so that I can set myPremise.GasMeter = myMeter, and retrieve myPremise in later code with myMeter.Premise?

Edit

When setting it up via the Fluent API as follows:

 protected override void OnModelCreating(DbModelBuilder modelBuilder)

{

modelBuilder.Entity<Premise>().HasOptional(p => p.GasMeter)

.WithRequired(m => m.Premise);

}

I get the following exception when running:

An exception of type 'System.Data.Entity.ModelConfiguration.ModelValidationException' occurred in EntityFramework.dll but was not handled in user code

Additional information: One or more validation errors were detected during model generation:

Premise: FromRole: NavigationProperty 'Premise' is not valid. Type 'GasMeter' of FromRole 'Premise_GasMeter_Target' in AssociationType 'Premise_GasMeter' must exactly match with the type 'Meter' on which this NavigationProperty is declared on.

Does this mean that I can't use Navigation Properties with inheritance?

How would I solve my problem then?

网友答案:

I think you need to add the Id to each class for PK/FK relationship

public abstract class Meter
{
   ....
    public int PremiseId
    public virtual Premise Premise { get; set; }
}

and

public class Premise
{
   ....
   public int GasMeterId
   public virtual GasMeter GasMeter{ get; set; }
}
网友答案:

You probably don't need to link these two entities as virtual properties within each other. Try modify Meter class to keep the PremiseId since it is the primary key of Premise table then get the Premise entity using Select(x => x.PremiseId == aMeter.PremiseId).SingleOrDefault() to get the Premise mapped to this GasMeter

public abstract class Meter
{
    public int MeterId { get; set; }
    public string EANNumber { get; set; }
    public string MeterNumber { get; set; }
    public int PremiseId { get; set; }
    public abstract void AddReading(CounterReading reading);
}

SET

var aPremise = new Premise();
var aMeter = new GasMeter();
aPremise.GasMeter = aMeter;
aMeter.PremiseId = aPremise.PremiseId;

GET

var thePremise = _repository.Set<Premise>.Select(x => x.PremiseId == aMeter.PremiseId).SingleOrDefault();
相关阅读:
Top