EF core code first, loose foreign key contraint after making it nullable











up vote
0
down vote

favorite












I had this:



public class Customer
{
public int Id {get; set;}
//some more properties
}

public class Payment
{
public int Id {get; set;}
public int CustomerId {get; set;} //foreign key
public Customer Customer {get; set;}
}


Which lead to a foreign key constraint in the database. Then I made the CustomerId nullable:



public class Payment
{
public int Id {get; set;}
public int? CustomerId {get; set;} //foreign key
public Customer Customer {get; set;}
}


If I generate the migration script, I get something like this:



IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'MyMigration')
BEGIN

DECLARE @var2 sysname;

SELECT @var2 = [d].[name]
FROM [sys].[default_constraints] [d]
INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id]
AND [d].[parent_object_id] = [c].[object_id]
WHERE ([d].[parent_object_id] = OBJECT_ID(N'[Payments]') AND [c].[name] = N'CustomerId');

IF @var2 IS NOT NULL EXEC(N'ALTER TABLE [Payments]
DROP CONSTRAINT [' + @var2 + '];');

ALTER TABLE [Payments] ALTER COLUMN [CustomerId] int NULL;
END;

GO


So now column Payments.CustomerId is nullable, but the foreign key constraint is removed. How can I get the foreign key constraint back?



edit: One more thing. Between my code changes, I also added this in protected override void OnModelCreating(ModelBuilder modelBuilder):



new PaymentMap(modelBuilder.Entity<Payment>());









share|improve this question
























  • Have you tried to add Public virtual ICollection<Payment> Payments {get;set;} inside Customer class?
    – Szymon Tomczyk
    Nov 8 at 12:07












  • The script looks incomplete. My test continues with ALTER TABLE [Payments] ADD CONSTRAINT [FK_Payments_Customers_CustomerId] FOREIGN KEY ([CustomerId]) REFERENCES [Customers] ([Id]) ON DELETE NO ACTION; and then inserting migration history record.
    – Ivan Stoev
    Nov 8 at 13:09










  • The script is incomplete, but mine lacks the part with "ADD CONSTRAINT [FK_Payments_Customers_CustomerId] FOREIGN KEY...". That's exactly why I asked my question here
    – Richard
    Nov 8 at 13:32










  • I've tested with EF Core 2.1.4 / SqlServer, what is your environment?
    – Ivan Stoev
    Nov 8 at 15:43

















up vote
0
down vote

favorite












I had this:



public class Customer
{
public int Id {get; set;}
//some more properties
}

public class Payment
{
public int Id {get; set;}
public int CustomerId {get; set;} //foreign key
public Customer Customer {get; set;}
}


Which lead to a foreign key constraint in the database. Then I made the CustomerId nullable:



public class Payment
{
public int Id {get; set;}
public int? CustomerId {get; set;} //foreign key
public Customer Customer {get; set;}
}


If I generate the migration script, I get something like this:



IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'MyMigration')
BEGIN

DECLARE @var2 sysname;

SELECT @var2 = [d].[name]
FROM [sys].[default_constraints] [d]
INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id]
AND [d].[parent_object_id] = [c].[object_id]
WHERE ([d].[parent_object_id] = OBJECT_ID(N'[Payments]') AND [c].[name] = N'CustomerId');

IF @var2 IS NOT NULL EXEC(N'ALTER TABLE [Payments]
DROP CONSTRAINT [' + @var2 + '];');

ALTER TABLE [Payments] ALTER COLUMN [CustomerId] int NULL;
END;

GO


So now column Payments.CustomerId is nullable, but the foreign key constraint is removed. How can I get the foreign key constraint back?



edit: One more thing. Between my code changes, I also added this in protected override void OnModelCreating(ModelBuilder modelBuilder):



new PaymentMap(modelBuilder.Entity<Payment>());









share|improve this question
























  • Have you tried to add Public virtual ICollection<Payment> Payments {get;set;} inside Customer class?
    – Szymon Tomczyk
    Nov 8 at 12:07












  • The script looks incomplete. My test continues with ALTER TABLE [Payments] ADD CONSTRAINT [FK_Payments_Customers_CustomerId] FOREIGN KEY ([CustomerId]) REFERENCES [Customers] ([Id]) ON DELETE NO ACTION; and then inserting migration history record.
    – Ivan Stoev
    Nov 8 at 13:09










  • The script is incomplete, but mine lacks the part with "ADD CONSTRAINT [FK_Payments_Customers_CustomerId] FOREIGN KEY...". That's exactly why I asked my question here
    – Richard
    Nov 8 at 13:32










  • I've tested with EF Core 2.1.4 / SqlServer, what is your environment?
    – Ivan Stoev
    Nov 8 at 15:43















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I had this:



public class Customer
{
public int Id {get; set;}
//some more properties
}

public class Payment
{
public int Id {get; set;}
public int CustomerId {get; set;} //foreign key
public Customer Customer {get; set;}
}


Which lead to a foreign key constraint in the database. Then I made the CustomerId nullable:



public class Payment
{
public int Id {get; set;}
public int? CustomerId {get; set;} //foreign key
public Customer Customer {get; set;}
}


If I generate the migration script, I get something like this:



IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'MyMigration')
BEGIN

DECLARE @var2 sysname;

SELECT @var2 = [d].[name]
FROM [sys].[default_constraints] [d]
INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id]
AND [d].[parent_object_id] = [c].[object_id]
WHERE ([d].[parent_object_id] = OBJECT_ID(N'[Payments]') AND [c].[name] = N'CustomerId');

IF @var2 IS NOT NULL EXEC(N'ALTER TABLE [Payments]
DROP CONSTRAINT [' + @var2 + '];');

ALTER TABLE [Payments] ALTER COLUMN [CustomerId] int NULL;
END;

GO


So now column Payments.CustomerId is nullable, but the foreign key constraint is removed. How can I get the foreign key constraint back?



edit: One more thing. Between my code changes, I also added this in protected override void OnModelCreating(ModelBuilder modelBuilder):



new PaymentMap(modelBuilder.Entity<Payment>());









share|improve this question















I had this:



public class Customer
{
public int Id {get; set;}
//some more properties
}

public class Payment
{
public int Id {get; set;}
public int CustomerId {get; set;} //foreign key
public Customer Customer {get; set;}
}


Which lead to a foreign key constraint in the database. Then I made the CustomerId nullable:



public class Payment
{
public int Id {get; set;}
public int? CustomerId {get; set;} //foreign key
public Customer Customer {get; set;}
}


If I generate the migration script, I get something like this:



IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'MyMigration')
BEGIN

DECLARE @var2 sysname;

SELECT @var2 = [d].[name]
FROM [sys].[default_constraints] [d]
INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id]
AND [d].[parent_object_id] = [c].[object_id]
WHERE ([d].[parent_object_id] = OBJECT_ID(N'[Payments]') AND [c].[name] = N'CustomerId');

IF @var2 IS NOT NULL EXEC(N'ALTER TABLE [Payments]
DROP CONSTRAINT [' + @var2 + '];');

ALTER TABLE [Payments] ALTER COLUMN [CustomerId] int NULL;
END;

GO


So now column Payments.CustomerId is nullable, but the foreign key constraint is removed. How can I get the foreign key constraint back?



edit: One more thing. Between my code changes, I also added this in protected override void OnModelCreating(ModelBuilder modelBuilder):



new PaymentMap(modelBuilder.Entity<Payment>());






c# ef-code-first ef-core-2.1






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 14:00

























asked Nov 8 at 11:29









Richard

62




62












  • Have you tried to add Public virtual ICollection<Payment> Payments {get;set;} inside Customer class?
    – Szymon Tomczyk
    Nov 8 at 12:07












  • The script looks incomplete. My test continues with ALTER TABLE [Payments] ADD CONSTRAINT [FK_Payments_Customers_CustomerId] FOREIGN KEY ([CustomerId]) REFERENCES [Customers] ([Id]) ON DELETE NO ACTION; and then inserting migration history record.
    – Ivan Stoev
    Nov 8 at 13:09










  • The script is incomplete, but mine lacks the part with "ADD CONSTRAINT [FK_Payments_Customers_CustomerId] FOREIGN KEY...". That's exactly why I asked my question here
    – Richard
    Nov 8 at 13:32










  • I've tested with EF Core 2.1.4 / SqlServer, what is your environment?
    – Ivan Stoev
    Nov 8 at 15:43




















  • Have you tried to add Public virtual ICollection<Payment> Payments {get;set;} inside Customer class?
    – Szymon Tomczyk
    Nov 8 at 12:07












  • The script looks incomplete. My test continues with ALTER TABLE [Payments] ADD CONSTRAINT [FK_Payments_Customers_CustomerId] FOREIGN KEY ([CustomerId]) REFERENCES [Customers] ([Id]) ON DELETE NO ACTION; and then inserting migration history record.
    – Ivan Stoev
    Nov 8 at 13:09










  • The script is incomplete, but mine lacks the part with "ADD CONSTRAINT [FK_Payments_Customers_CustomerId] FOREIGN KEY...". That's exactly why I asked my question here
    – Richard
    Nov 8 at 13:32










  • I've tested with EF Core 2.1.4 / SqlServer, what is your environment?
    – Ivan Stoev
    Nov 8 at 15:43


















Have you tried to add Public virtual ICollection<Payment> Payments {get;set;} inside Customer class?
– Szymon Tomczyk
Nov 8 at 12:07






Have you tried to add Public virtual ICollection<Payment> Payments {get;set;} inside Customer class?
– Szymon Tomczyk
Nov 8 at 12:07














The script looks incomplete. My test continues with ALTER TABLE [Payments] ADD CONSTRAINT [FK_Payments_Customers_CustomerId] FOREIGN KEY ([CustomerId]) REFERENCES [Customers] ([Id]) ON DELETE NO ACTION; and then inserting migration history record.
– Ivan Stoev
Nov 8 at 13:09




The script looks incomplete. My test continues with ALTER TABLE [Payments] ADD CONSTRAINT [FK_Payments_Customers_CustomerId] FOREIGN KEY ([CustomerId]) REFERENCES [Customers] ([Id]) ON DELETE NO ACTION; and then inserting migration history record.
– Ivan Stoev
Nov 8 at 13:09












The script is incomplete, but mine lacks the part with "ADD CONSTRAINT [FK_Payments_Customers_CustomerId] FOREIGN KEY...". That's exactly why I asked my question here
– Richard
Nov 8 at 13:32




The script is incomplete, but mine lacks the part with "ADD CONSTRAINT [FK_Payments_Customers_CustomerId] FOREIGN KEY...". That's exactly why I asked my question here
– Richard
Nov 8 at 13:32












I've tested with EF Core 2.1.4 / SqlServer, what is your environment?
– Ivan Stoev
Nov 8 at 15:43






I've tested with EF Core 2.1.4 / SqlServer, what is your environment?
– Ivan Stoev
Nov 8 at 15:43














4 Answers
4






active

oldest

votes

















up vote
0
down vote













You need to annotate it with System.ComponentModel.DataAnnotations.Schema.ForeignKeyAttribute explicitly:



public class Payment
{
public int Id {get; set;}

public int? CustomerId {get; set;} //foreign key

[ForeignKey("CustomerId")]
public Customer Customer {get; set;}
}





share|improve this answer





















  • There is no need to add ForeignKeyAttribute, if naming conventions are fine. entityframeworktutorial.net/efcore/conventions-in-ef-core.aspx
    – Szymon Tomczyk
    Nov 8 at 12:06










  • It worked without [ForeignKey("CustomerId")] before, in the old situation.
    – Richard
    Nov 8 at 13:14












  • @Richard, as far as I know, naming conventions works when type of foreign key property is matching type of primary key of relative class. And it works like a charm before. But now, int? (nullable int) type of Payment.CustomerId doesn't match int type of Customer.Id so EF need your hint about this.
    – vasily.sib
    Nov 8 at 17:31




















up vote
0
down vote













In order to make it work you should add public virtual ICollection<Tenant> Payments { get; set;} to Customer class



Blockquote



public class Customer
{
public int Id { get; set; }
public virtual ICollection<Payment> Payments { get; set; }
}

public class Payment
{
public int PaymentId{ get; set; }
public int? CustomerId { get; set; }
public Customer Customer { get; set; }
}





share|improve this answer





















  • This doesn't do the trick. I still miss my foreign key constraint.
    – Richard
    Nov 8 at 13:38










  • virtual ICollection<T> stuff are primary is for lazy loading
    – vasily.sib
    Nov 8 at 17:33




















up vote
0
down vote













As EF Core requires manually making the connections between tables, have you tried to write them down in your DataContext class?



Add List<Payment> Payments in your Customer class and in your DataContext class in the OnModelCreate override method add:



builder
.Entity<Payment>()
.HasOne(p => p.Customer)
.WithMany(c => c.Payments)
.HasForeignKey(p => p.CustomerId);





share|improve this answer





















  • "EF Core requires manually making the connections between tables" - this is not true. Most of the time there is an applicable convention
    – vasily.sib
    Nov 8 at 17:39


















up vote
0
down vote













I dropped my local database and re-created it with "update-database". That solved my problem. Thanks everybody for their time and effort!






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206854%2fef-core-code-first-loose-foreign-key-contraint-after-making-it-nullable%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    You need to annotate it with System.ComponentModel.DataAnnotations.Schema.ForeignKeyAttribute explicitly:



    public class Payment
    {
    public int Id {get; set;}

    public int? CustomerId {get; set;} //foreign key

    [ForeignKey("CustomerId")]
    public Customer Customer {get; set;}
    }





    share|improve this answer





















    • There is no need to add ForeignKeyAttribute, if naming conventions are fine. entityframeworktutorial.net/efcore/conventions-in-ef-core.aspx
      – Szymon Tomczyk
      Nov 8 at 12:06










    • It worked without [ForeignKey("CustomerId")] before, in the old situation.
      – Richard
      Nov 8 at 13:14












    • @Richard, as far as I know, naming conventions works when type of foreign key property is matching type of primary key of relative class. And it works like a charm before. But now, int? (nullable int) type of Payment.CustomerId doesn't match int type of Customer.Id so EF need your hint about this.
      – vasily.sib
      Nov 8 at 17:31

















    up vote
    0
    down vote













    You need to annotate it with System.ComponentModel.DataAnnotations.Schema.ForeignKeyAttribute explicitly:



    public class Payment
    {
    public int Id {get; set;}

    public int? CustomerId {get; set;} //foreign key

    [ForeignKey("CustomerId")]
    public Customer Customer {get; set;}
    }





    share|improve this answer





















    • There is no need to add ForeignKeyAttribute, if naming conventions are fine. entityframeworktutorial.net/efcore/conventions-in-ef-core.aspx
      – Szymon Tomczyk
      Nov 8 at 12:06










    • It worked without [ForeignKey("CustomerId")] before, in the old situation.
      – Richard
      Nov 8 at 13:14












    • @Richard, as far as I know, naming conventions works when type of foreign key property is matching type of primary key of relative class. And it works like a charm before. But now, int? (nullable int) type of Payment.CustomerId doesn't match int type of Customer.Id so EF need your hint about this.
      – vasily.sib
      Nov 8 at 17:31















    up vote
    0
    down vote










    up vote
    0
    down vote









    You need to annotate it with System.ComponentModel.DataAnnotations.Schema.ForeignKeyAttribute explicitly:



    public class Payment
    {
    public int Id {get; set;}

    public int? CustomerId {get; set;} //foreign key

    [ForeignKey("CustomerId")]
    public Customer Customer {get; set;}
    }





    share|improve this answer












    You need to annotate it with System.ComponentModel.DataAnnotations.Schema.ForeignKeyAttribute explicitly:



    public class Payment
    {
    public int Id {get; set;}

    public int? CustomerId {get; set;} //foreign key

    [ForeignKey("CustomerId")]
    public Customer Customer {get; set;}
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 8 at 11:33









    vasily.sib

    1,5971716




    1,5971716












    • There is no need to add ForeignKeyAttribute, if naming conventions are fine. entityframeworktutorial.net/efcore/conventions-in-ef-core.aspx
      – Szymon Tomczyk
      Nov 8 at 12:06










    • It worked without [ForeignKey("CustomerId")] before, in the old situation.
      – Richard
      Nov 8 at 13:14












    • @Richard, as far as I know, naming conventions works when type of foreign key property is matching type of primary key of relative class. And it works like a charm before. But now, int? (nullable int) type of Payment.CustomerId doesn't match int type of Customer.Id so EF need your hint about this.
      – vasily.sib
      Nov 8 at 17:31




















    • There is no need to add ForeignKeyAttribute, if naming conventions are fine. entityframeworktutorial.net/efcore/conventions-in-ef-core.aspx
      – Szymon Tomczyk
      Nov 8 at 12:06










    • It worked without [ForeignKey("CustomerId")] before, in the old situation.
      – Richard
      Nov 8 at 13:14












    • @Richard, as far as I know, naming conventions works when type of foreign key property is matching type of primary key of relative class. And it works like a charm before. But now, int? (nullable int) type of Payment.CustomerId doesn't match int type of Customer.Id so EF need your hint about this.
      – vasily.sib
      Nov 8 at 17:31


















    There is no need to add ForeignKeyAttribute, if naming conventions are fine. entityframeworktutorial.net/efcore/conventions-in-ef-core.aspx
    – Szymon Tomczyk
    Nov 8 at 12:06




    There is no need to add ForeignKeyAttribute, if naming conventions are fine. entityframeworktutorial.net/efcore/conventions-in-ef-core.aspx
    – Szymon Tomczyk
    Nov 8 at 12:06












    It worked without [ForeignKey("CustomerId")] before, in the old situation.
    – Richard
    Nov 8 at 13:14






    It worked without [ForeignKey("CustomerId")] before, in the old situation.
    – Richard
    Nov 8 at 13:14














    @Richard, as far as I know, naming conventions works when type of foreign key property is matching type of primary key of relative class. And it works like a charm before. But now, int? (nullable int) type of Payment.CustomerId doesn't match int type of Customer.Id so EF need your hint about this.
    – vasily.sib
    Nov 8 at 17:31






    @Richard, as far as I know, naming conventions works when type of foreign key property is matching type of primary key of relative class. And it works like a charm before. But now, int? (nullable int) type of Payment.CustomerId doesn't match int type of Customer.Id so EF need your hint about this.
    – vasily.sib
    Nov 8 at 17:31














    up vote
    0
    down vote













    In order to make it work you should add public virtual ICollection<Tenant> Payments { get; set;} to Customer class



    Blockquote



    public class Customer
    {
    public int Id { get; set; }
    public virtual ICollection<Payment> Payments { get; set; }
    }

    public class Payment
    {
    public int PaymentId{ get; set; }
    public int? CustomerId { get; set; }
    public Customer Customer { get; set; }
    }





    share|improve this answer





















    • This doesn't do the trick. I still miss my foreign key constraint.
      – Richard
      Nov 8 at 13:38










    • virtual ICollection<T> stuff are primary is for lazy loading
      – vasily.sib
      Nov 8 at 17:33

















    up vote
    0
    down vote













    In order to make it work you should add public virtual ICollection<Tenant> Payments { get; set;} to Customer class



    Blockquote



    public class Customer
    {
    public int Id { get; set; }
    public virtual ICollection<Payment> Payments { get; set; }
    }

    public class Payment
    {
    public int PaymentId{ get; set; }
    public int? CustomerId { get; set; }
    public Customer Customer { get; set; }
    }





    share|improve this answer





















    • This doesn't do the trick. I still miss my foreign key constraint.
      – Richard
      Nov 8 at 13:38










    • virtual ICollection<T> stuff are primary is for lazy loading
      – vasily.sib
      Nov 8 at 17:33















    up vote
    0
    down vote










    up vote
    0
    down vote









    In order to make it work you should add public virtual ICollection<Tenant> Payments { get; set;} to Customer class



    Blockquote



    public class Customer
    {
    public int Id { get; set; }
    public virtual ICollection<Payment> Payments { get; set; }
    }

    public class Payment
    {
    public int PaymentId{ get; set; }
    public int? CustomerId { get; set; }
    public Customer Customer { get; set; }
    }





    share|improve this answer












    In order to make it work you should add public virtual ICollection<Tenant> Payments { get; set;} to Customer class



    Blockquote



    public class Customer
    {
    public int Id { get; set; }
    public virtual ICollection<Payment> Payments { get; set; }
    }

    public class Payment
    {
    public int PaymentId{ get; set; }
    public int? CustomerId { get; set; }
    public Customer Customer { get; set; }
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 8 at 12:11









    Szymon Tomczyk

    1213




    1213












    • This doesn't do the trick. I still miss my foreign key constraint.
      – Richard
      Nov 8 at 13:38










    • virtual ICollection<T> stuff are primary is for lazy loading
      – vasily.sib
      Nov 8 at 17:33




















    • This doesn't do the trick. I still miss my foreign key constraint.
      – Richard
      Nov 8 at 13:38










    • virtual ICollection<T> stuff are primary is for lazy loading
      – vasily.sib
      Nov 8 at 17:33


















    This doesn't do the trick. I still miss my foreign key constraint.
    – Richard
    Nov 8 at 13:38




    This doesn't do the trick. I still miss my foreign key constraint.
    – Richard
    Nov 8 at 13:38












    virtual ICollection<T> stuff are primary is for lazy loading
    – vasily.sib
    Nov 8 at 17:33






    virtual ICollection<T> stuff are primary is for lazy loading
    – vasily.sib
    Nov 8 at 17:33












    up vote
    0
    down vote













    As EF Core requires manually making the connections between tables, have you tried to write them down in your DataContext class?



    Add List<Payment> Payments in your Customer class and in your DataContext class in the OnModelCreate override method add:



    builder
    .Entity<Payment>()
    .HasOne(p => p.Customer)
    .WithMany(c => c.Payments)
    .HasForeignKey(p => p.CustomerId);





    share|improve this answer





















    • "EF Core requires manually making the connections between tables" - this is not true. Most of the time there is an applicable convention
      – vasily.sib
      Nov 8 at 17:39















    up vote
    0
    down vote













    As EF Core requires manually making the connections between tables, have you tried to write them down in your DataContext class?



    Add List<Payment> Payments in your Customer class and in your DataContext class in the OnModelCreate override method add:



    builder
    .Entity<Payment>()
    .HasOne(p => p.Customer)
    .WithMany(c => c.Payments)
    .HasForeignKey(p => p.CustomerId);





    share|improve this answer





















    • "EF Core requires manually making the connections between tables" - this is not true. Most of the time there is an applicable convention
      – vasily.sib
      Nov 8 at 17:39













    up vote
    0
    down vote










    up vote
    0
    down vote









    As EF Core requires manually making the connections between tables, have you tried to write them down in your DataContext class?



    Add List<Payment> Payments in your Customer class and in your DataContext class in the OnModelCreate override method add:



    builder
    .Entity<Payment>()
    .HasOne(p => p.Customer)
    .WithMany(c => c.Payments)
    .HasForeignKey(p => p.CustomerId);





    share|improve this answer












    As EF Core requires manually making the connections between tables, have you tried to write them down in your DataContext class?



    Add List<Payment> Payments in your Customer class and in your DataContext class in the OnModelCreate override method add:



    builder
    .Entity<Payment>()
    .HasOne(p => p.Customer)
    .WithMany(c => c.Payments)
    .HasForeignKey(p => p.CustomerId);






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 8 at 15:06









    Iavor Orlyov

    104




    104












    • "EF Core requires manually making the connections between tables" - this is not true. Most of the time there is an applicable convention
      – vasily.sib
      Nov 8 at 17:39


















    • "EF Core requires manually making the connections between tables" - this is not true. Most of the time there is an applicable convention
      – vasily.sib
      Nov 8 at 17:39
















    "EF Core requires manually making the connections between tables" - this is not true. Most of the time there is an applicable convention
    – vasily.sib
    Nov 8 at 17:39




    "EF Core requires manually making the connections between tables" - this is not true. Most of the time there is an applicable convention
    – vasily.sib
    Nov 8 at 17:39










    up vote
    0
    down vote













    I dropped my local database and re-created it with "update-database". That solved my problem. Thanks everybody for their time and effort!






    share|improve this answer

























      up vote
      0
      down vote













      I dropped my local database and re-created it with "update-database". That solved my problem. Thanks everybody for their time and effort!






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        I dropped my local database and re-created it with "update-database". That solved my problem. Thanks everybody for their time and effort!






        share|improve this answer












        I dropped my local database and re-created it with "update-database". That solved my problem. Thanks everybody for their time and effort!







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 at 14:58









        Richard

        62




        62






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206854%2fef-core-code-first-loose-foreign-key-contraint-after-making-it-nullable%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Schultheiß

            Verwaltungsgliederung Dänemarks

            Liste der Kulturdenkmale in Wilsdruff