Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Entity framework core - Relation many to one with nullable foreign key #16371

Closed
ghost opened this issue Jul 1, 2019 · 4 comments
Closed

Entity framework core - Relation many to one with nullable foreign key #16371

ghost opened this issue Jul 1, 2019 · 4 comments

Comments

@ghost
Copy link

ghost commented Jul 1, 2019

I have two tables in my database :

public class PARENT
{
    public string ID_PARENT { get; set; }    

    public int? ID_PARENT_BIS { get; set; }    

    public List<CHILD> CHILDREN { get; set; } 
}

This table has a primary key : ID_PARENT. The ID_PARENT_BIS is used into several relationships.

public class CHILD
{
    [Required]
    public string PID_CHILD { get; set; } 

    public int ID { get; set; }

    public int? ID_PARENT_BIS { get; set; } 

    [Required]
    public string ID_EXTCHILD { get; set; } 
}

Between both, there are a relationships many to one. Here is the mapping :

modelBuilder.Entity<PARENT>(entity =>
        {

            entity.HasKey(e => e.ID_PARENT);

            entity.HasMany(a => a.CHILDREN)
                .WithOne()
                .HasForeignKey(i => i.ID_PARENT_BIS)
                .HasPrincipalKey(i => i.ID_PARENT_BIS);
        });

The mapping is correct but when I want to retrieve all elements of PARENT, I have the following error :

An unhandled exception occurred while processing the request. InvalidCastException: La colonne contient des données NULL (Column contain null data)

My relation need to be configured to work with ID_PARENT_BIS, not ID_PARENT. Because ID_PARENT_BIS could be null in database. This is a prerequisites of the project.

So is it really possible to do that with entity framework core ?

Thanks !

@ajcvickers
Copy link
Member

@stynn To clarify, ID_PARENT_BIS is a nullable column in the database, but you want to use this as an alternate key for the principal end of the relationship? if so, this is a duplicate of issue #4415

@ghost
Copy link
Author

ghost commented Jul 3, 2019

@ajcvickers In fact, ID_PARENT_BIS is a nullable column in database and I want to use it into a relation many to one. If the PARENT table has no CHILD, the ID_PARENT_BIS is null in my database. There is a way to map this in EF Core ?

@ajcvickers
Copy link
Member

@stynn I'm not sure I am following completely. Can you post some examples of different combinations of nulls and keys database rows and describe how they should map to the properties of your objects?

@ajcvickers
Copy link
Member

EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it.

BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant