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

            Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

            Understanding generators in Python2019 Community Moderator ElectionGenerator function not working pythonFor loop not executing two timesGenerators - Printing generated valuesWhy can a python generator only be used once?What exactly do generators do?What does the “yield” keyword do?What does “list comprehension” mean? How does it work and how can I use it?sklearn Kfold acces single fold instead of for loopIs a generator the callable? Which is the generator?Apply Border To Range Of Cells Using OpenpyxlCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Understanding slice notationUnderstanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?

            How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page