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;








1
















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!










share|improve this 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 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


















1
















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!










share|improve this 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 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














1












1








1









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!










share|improve this question

















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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


















  • 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

















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













1 Answer
1






active

oldest

votes


















0














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!






share|improve this answer





























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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!






    share|improve this answer



























      0














      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!






      share|improve this answer

























        0












        0








        0







        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!






        share|improve this answer













        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!







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 9 at 2:32









        TanvirArjelTanvirArjel

        9,84732148




        9,84732148















            Popular posts from this blog

            Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

            Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

            2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived