How to create two one-to-one relations with same entity in Entity Framework Core [duplicate]Entity Framework Code First - two Foreign Keys from same tableModelling folder structure in Entity Framework CoreHow to setup entity relations in Entity Framework CoreError when trying to insert entity using .NET Core, Entity Framework and SQL Server on AzureGetting SqlException for a foreign key when building database on initThe collection argument 'foreignKeyPropertyNames' must contain at least one elementConventions.Remove<OneToManyCascadeDeleteConvention>() in EF Core?EF Core - self referencing entityWhat is the permission needed for a SQL Server login to change a tables name?EF Core 2.0 Trouble 'Cascading' Inserts for Related Entities When Updating Principle EntityEF trying to create new entry instead of relation
Why did the Germans forbid the possession of pet pigeons in Rostov-on-Don in 1941?
can i play a electric guitar through a bass amp?
Has the BBC provided arguments for saying Brexit being cancelled is unlikely?
Why can't I see bouncing of a switch on an oscilloscope?
Why are 150k or 200k jobs considered good when there are 300k+ births a month?
What does it mean to describe someone as a butt steak?
How is it possible to have an ability score that is less than 3?
Can a Warlock become Neutral Good?
Modeling an IPv4 Address
strToHex ( string to its hex representation as string)
Why doesn't H₄O²⁺ exist?
What's the point of deactivating Num Lock on login screens?
Is it possible to do 50 km distance without any previous training?
Why dont electromagnetic waves interact with each other?
Why was the small council so happy for Tyrion to become the Master of Coin?
Have astronauts in space suits ever taken selfies? If so, how?
How is the claim "I am in New York only if I am in America" the same as "If I am in New York, then I am in America?
Languages that we cannot (dis)prove to be Context-Free
"to be prejudice towards/against someone" vs "to be prejudiced against/towards someone"
Why do I get two different answers for this counting problem?
In Japanese, what’s the difference between “Tonari ni” (となりに) and “Tsugi” (つぎ)? When would you use one over the other?
Is it unprofessional to ask if a job posting on GlassDoor is real?
Why did Neo believe he could trust the machine when he asked for peace?
Mathematical cryptic clues
How to create two one-to-one relations with same entity in Entity Framework Core [duplicate]
Entity Framework Code First - two Foreign Keys from same tableModelling folder structure in Entity Framework CoreHow to setup entity relations in Entity Framework CoreError when trying to insert entity using .NET Core, Entity Framework and SQL Server on AzureGetting SqlException for a foreign key when building database on initThe collection argument 'foreignKeyPropertyNames' must contain at least one elementConventions.Remove<OneToManyCascadeDeleteConvention>() in EF Core?EF Core - self referencing entityWhat is the permission needed for a SQL Server login to change a tables name?EF Core 2.0 Trouble 'Cascading' Inserts for Related Entities When Updating Principle EntityEF trying to create new entry instead of relation
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
This question already has an answer here:
Entity Framework Code First - two Foreign Keys from same table
5 answers
I'm getting stuck when using code-first in EF Core 2.2.
I have an entity called: AirSensor which I need to have two one-to-one relations to TemperatureSensors, as the AirSensor is capable of read Temperature and dew point temperature.
Therefore my code is here:
public class AirSensor
public long AirSensorId get; set;
public TemperatureSensor TemperatureSensor get; set;
public HumiditySensor Humidity get; set;
public Enthalpy EnthalpyCalc get; set;
public TemperatureSensor DewPointTemperatureSensor get; set;
public long TemperatureSensorId get; set;
public long HumiditySensorId get; set;
public long EnthalpyId get; set;
public long DewPointTemperatureSensorId get; set;
And Temperature Entity is:
public class TemperatureSensor
public long TemperatureSensorId get; set;
public string Name get; set;
public float CurrentValue get; set;
public DateTime LastUpdated get; set;
public ICollection<TemperatureMeasurement> Measurements get; set;
When I add the migration everything is ok, but when updating the database it fails:
Failed executing DbCommand (17ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
ALTER TABLE [AirSensors] ADD CONSTRAINT [FK_AirSensors_TemperatureSensors_DewPointSensorId] FOREIGN KEY ([DewPointSensorId]) REFERENCES [TemperatureSensors] ([TemperatureSensorId]) ON DELETE CASCADE;
System.Data.SqlClient.SqlException (0x80131904): Si especifica la restricción FOREIGN KEY 'FK_AirSensors_TemperatureSensors_DewPointSensorId' en la tabla 'AirSensors', podrían producirse ciclos o múltiples rutas en cascada. Especifique ON DELETE NO ACTION o UPDATE NO ACTION, o bien modifique otras restricciones FOREIGN KEY.
However, I don't see the problem here, I just want to have two separate one-to-one relations.
What am I missing?
Many thanks in advance!
asp.net-core entity-framework-core ef-core-2.2
marked as duplicate by Community♦ Mar 12 at 6:44
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Entity Framework Code First - two Foreign Keys from same table
5 answers
I'm getting stuck when using code-first in EF Core 2.2.
I have an entity called: AirSensor which I need to have two one-to-one relations to TemperatureSensors, as the AirSensor is capable of read Temperature and dew point temperature.
Therefore my code is here:
public class AirSensor
public long AirSensorId get; set;
public TemperatureSensor TemperatureSensor get; set;
public HumiditySensor Humidity get; set;
public Enthalpy EnthalpyCalc get; set;
public TemperatureSensor DewPointTemperatureSensor get; set;
public long TemperatureSensorId get; set;
public long HumiditySensorId get; set;
public long EnthalpyId get; set;
public long DewPointTemperatureSensorId get; set;
And Temperature Entity is:
public class TemperatureSensor
public long TemperatureSensorId get; set;
public string Name get; set;
public float CurrentValue get; set;
public DateTime LastUpdated get; set;
public ICollection<TemperatureMeasurement> Measurements get; set;
When I add the migration everything is ok, but when updating the database it fails:
Failed executing DbCommand (17ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
ALTER TABLE [AirSensors] ADD CONSTRAINT [FK_AirSensors_TemperatureSensors_DewPointSensorId] FOREIGN KEY ([DewPointSensorId]) REFERENCES [TemperatureSensors] ([TemperatureSensorId]) ON DELETE CASCADE;
System.Data.SqlClient.SqlException (0x80131904): Si especifica la restricción FOREIGN KEY 'FK_AirSensors_TemperatureSensors_DewPointSensorId' en la tabla 'AirSensors', podrían producirse ciclos o múltiples rutas en cascada. Especifique ON DELETE NO ACTION o UPDATE NO ACTION, o bien modifique otras restricciones FOREIGN KEY.
However, I don't see the problem here, I just want to have two separate one-to-one relations.
What am I missing?
Many thanks in advance!
asp.net-core entity-framework-core ef-core-2.2
marked as duplicate by Community♦ Mar 12 at 6:44
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Are you doing any custom mapping in your dbcontext? You likely have to explicitly mark your ForeignKeys on the AirSensor class.
– jcruz
Mar 8 at 18:55
No custom mapping. But the funny thing is that it works with just one relation one to one
– cjbs
Mar 8 at 18:59
1
see this question, particularly the fluent API mapping withinOnModelCreating
: stackoverflow.com/questions/5559043/…
– jcruz
Mar 8 at 19:02
let me know if that does it or if you need further help
– jcruz
Mar 8 at 19:33
add a comment |
This question already has an answer here:
Entity Framework Code First - two Foreign Keys from same table
5 answers
I'm getting stuck when using code-first in EF Core 2.2.
I have an entity called: AirSensor which I need to have two one-to-one relations to TemperatureSensors, as the AirSensor is capable of read Temperature and dew point temperature.
Therefore my code is here:
public class AirSensor
public long AirSensorId get; set;
public TemperatureSensor TemperatureSensor get; set;
public HumiditySensor Humidity get; set;
public Enthalpy EnthalpyCalc get; set;
public TemperatureSensor DewPointTemperatureSensor get; set;
public long TemperatureSensorId get; set;
public long HumiditySensorId get; set;
public long EnthalpyId get; set;
public long DewPointTemperatureSensorId get; set;
And Temperature Entity is:
public class TemperatureSensor
public long TemperatureSensorId get; set;
public string Name get; set;
public float CurrentValue get; set;
public DateTime LastUpdated get; set;
public ICollection<TemperatureMeasurement> Measurements get; set;
When I add the migration everything is ok, but when updating the database it fails:
Failed executing DbCommand (17ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
ALTER TABLE [AirSensors] ADD CONSTRAINT [FK_AirSensors_TemperatureSensors_DewPointSensorId] FOREIGN KEY ([DewPointSensorId]) REFERENCES [TemperatureSensors] ([TemperatureSensorId]) ON DELETE CASCADE;
System.Data.SqlClient.SqlException (0x80131904): Si especifica la restricción FOREIGN KEY 'FK_AirSensors_TemperatureSensors_DewPointSensorId' en la tabla 'AirSensors', podrían producirse ciclos o múltiples rutas en cascada. Especifique ON DELETE NO ACTION o UPDATE NO ACTION, o bien modifique otras restricciones FOREIGN KEY.
However, I don't see the problem here, I just want to have two separate one-to-one relations.
What am I missing?
Many thanks in advance!
asp.net-core entity-framework-core ef-core-2.2
This question already has an answer here:
Entity Framework Code First - two Foreign Keys from same table
5 answers
I'm getting stuck when using code-first in EF Core 2.2.
I have an entity called: AirSensor which I need to have two one-to-one relations to TemperatureSensors, as the AirSensor is capable of read Temperature and dew point temperature.
Therefore my code is here:
public class AirSensor
public long AirSensorId get; set;
public TemperatureSensor TemperatureSensor get; set;
public HumiditySensor Humidity get; set;
public Enthalpy EnthalpyCalc get; set;
public TemperatureSensor DewPointTemperatureSensor get; set;
public long TemperatureSensorId get; set;
public long HumiditySensorId get; set;
public long EnthalpyId get; set;
public long DewPointTemperatureSensorId get; set;
And Temperature Entity is:
public class TemperatureSensor
public long TemperatureSensorId get; set;
public string Name get; set;
public float CurrentValue get; set;
public DateTime LastUpdated get; set;
public ICollection<TemperatureMeasurement> Measurements get; set;
When I add the migration everything is ok, but when updating the database it fails:
Failed executing DbCommand (17ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
ALTER TABLE [AirSensors] ADD CONSTRAINT [FK_AirSensors_TemperatureSensors_DewPointSensorId] FOREIGN KEY ([DewPointSensorId]) REFERENCES [TemperatureSensors] ([TemperatureSensorId]) ON DELETE CASCADE;
System.Data.SqlClient.SqlException (0x80131904): Si especifica la restricción FOREIGN KEY 'FK_AirSensors_TemperatureSensors_DewPointSensorId' en la tabla 'AirSensors', podrían producirse ciclos o múltiples rutas en cascada. Especifique ON DELETE NO ACTION o UPDATE NO ACTION, o bien modifique otras restricciones FOREIGN KEY.
However, I don't see the problem here, I just want to have two separate one-to-one relations.
What am I missing?
Many thanks in advance!
This question already has an answer here:
Entity Framework Code First - two Foreign Keys from same table
5 answers
asp.net-core entity-framework-core ef-core-2.2
asp.net-core entity-framework-core ef-core-2.2
edited Mar 9 at 2:36
TanvirArjel
9,84732148
9,84732148
asked Mar 8 at 18:18
cjbscjbs
861111
861111
marked as duplicate by Community♦ Mar 12 at 6:44
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Community♦ Mar 12 at 6:44
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Are you doing any custom mapping in your dbcontext? You likely have to explicitly mark your ForeignKeys on the AirSensor class.
– jcruz
Mar 8 at 18:55
No custom mapping. But the funny thing is that it works with just one relation one to one
– cjbs
Mar 8 at 18:59
1
see this question, particularly the fluent API mapping withinOnModelCreating
: stackoverflow.com/questions/5559043/…
– jcruz
Mar 8 at 19:02
let me know if that does it or if you need further help
– jcruz
Mar 8 at 19:33
add a comment |
Are you doing any custom mapping in your dbcontext? You likely have to explicitly mark your ForeignKeys on the AirSensor class.
– jcruz
Mar 8 at 18:55
No custom mapping. But the funny thing is that it works with just one relation one to one
– cjbs
Mar 8 at 18:59
1
see this question, particularly the fluent API mapping withinOnModelCreating
: stackoverflow.com/questions/5559043/…
– jcruz
Mar 8 at 19:02
let me know if that does it or if you need further help
– jcruz
Mar 8 at 19:33
Are you doing any custom mapping in your dbcontext? You likely have to explicitly mark your ForeignKeys on the AirSensor class.
– jcruz
Mar 8 at 18:55
Are you doing any custom mapping in your dbcontext? You likely have to explicitly mark your ForeignKeys on the AirSensor class.
– jcruz
Mar 8 at 18:55
No custom mapping. But the funny thing is that it works with just one relation one to one
– cjbs
Mar 8 at 18:59
No custom mapping. But the funny thing is that it works with just one relation one to one
– cjbs
Mar 8 at 18:59
1
1
see this question, particularly the fluent API mapping within
OnModelCreating
: stackoverflow.com/questions/5559043/…– jcruz
Mar 8 at 19:02
see this question, particularly the fluent API mapping within
OnModelCreating
: stackoverflow.com/questions/5559043/…– jcruz
Mar 8 at 19:02
let me know if that does it or if you need further help
– jcruz
Mar 8 at 19:33
let me know if that does it or if you need further help
– jcruz
Mar 8 at 19:33
add a comment |
1 Answer
1
active
oldest
votes
Configure AirSensor
with Fluent API
as follows:
protected override void OnModelCreating(ModelBuilder modelBuilder)
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<AirSensor>().HasOne(ars => ars.TemperatureSensor)
.WithOne().HasForeignKey<AirSensor>(ars => ars.TemperatureSensorId)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<AirSensor>().HasOne(ars => ars.DewPointTemperatureSensor)
.WithOne().HasForeignKey<AirSensor>(ars => ars.DewPointTemperatureSensorId)
.OnDelete(DeleteBehavior.Restrict);
Now it will work as expected!
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Configure AirSensor
with Fluent API
as follows:
protected override void OnModelCreating(ModelBuilder modelBuilder)
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<AirSensor>().HasOne(ars => ars.TemperatureSensor)
.WithOne().HasForeignKey<AirSensor>(ars => ars.TemperatureSensorId)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<AirSensor>().HasOne(ars => ars.DewPointTemperatureSensor)
.WithOne().HasForeignKey<AirSensor>(ars => ars.DewPointTemperatureSensorId)
.OnDelete(DeleteBehavior.Restrict);
Now it will work as expected!
add a comment |
Configure AirSensor
with Fluent API
as follows:
protected override void OnModelCreating(ModelBuilder modelBuilder)
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<AirSensor>().HasOne(ars => ars.TemperatureSensor)
.WithOne().HasForeignKey<AirSensor>(ars => ars.TemperatureSensorId)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<AirSensor>().HasOne(ars => ars.DewPointTemperatureSensor)
.WithOne().HasForeignKey<AirSensor>(ars => ars.DewPointTemperatureSensorId)
.OnDelete(DeleteBehavior.Restrict);
Now it will work as expected!
add a comment |
Configure AirSensor
with Fluent API
as follows:
protected override void OnModelCreating(ModelBuilder modelBuilder)
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<AirSensor>().HasOne(ars => ars.TemperatureSensor)
.WithOne().HasForeignKey<AirSensor>(ars => ars.TemperatureSensorId)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<AirSensor>().HasOne(ars => ars.DewPointTemperatureSensor)
.WithOne().HasForeignKey<AirSensor>(ars => ars.DewPointTemperatureSensorId)
.OnDelete(DeleteBehavior.Restrict);
Now it will work as expected!
Configure AirSensor
with Fluent API
as follows:
protected override void OnModelCreating(ModelBuilder modelBuilder)
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<AirSensor>().HasOne(ars => ars.TemperatureSensor)
.WithOne().HasForeignKey<AirSensor>(ars => ars.TemperatureSensorId)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<AirSensor>().HasOne(ars => ars.DewPointTemperatureSensor)
.WithOne().HasForeignKey<AirSensor>(ars => ars.DewPointTemperatureSensorId)
.OnDelete(DeleteBehavior.Restrict);
Now it will work as expected!
answered Mar 9 at 2:32
TanvirArjelTanvirArjel
9,84732148
9,84732148
add a comment |
add a comment |
Are you doing any custom mapping in your dbcontext? You likely have to explicitly mark your ForeignKeys on the AirSensor class.
– jcruz
Mar 8 at 18:55
No custom mapping. But the funny thing is that it works with just one relation one to one
– cjbs
Mar 8 at 18:59
1
see this question, particularly the fluent API mapping within
OnModelCreating
: stackoverflow.com/questions/5559043/…– jcruz
Mar 8 at 19:02
let me know if that does it or if you need further help
– jcruz
Mar 8 at 19:33