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>());
c# ef-code-first ef-core-2.1
add a comment |
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>());
c# ef-code-first ef-core-2.1
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 withALTER 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
add a comment |
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>());
c# ef-code-first ef-core-2.1
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
c# ef-code-first ef-core-2.1
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 withALTER 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
add a comment |
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 withALTER 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
add a comment |
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;}
}
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 ofPayment.CustomerId
doesn't matchint
type ofCustomer.Id
so EF need your hint about this.
– vasily.sib
Nov 8 at 17:31
add a comment |
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; }
}
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
add a comment |
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);
"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
add a comment |
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!
add a comment |
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;}
}
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 ofPayment.CustomerId
doesn't matchint
type ofCustomer.Id
so EF need your hint about this.
– vasily.sib
Nov 8 at 17:31
add a comment |
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;}
}
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 ofPayment.CustomerId
doesn't matchint
type ofCustomer.Id
so EF need your hint about this.
– vasily.sib
Nov 8 at 17:31
add a comment |
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;}
}
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;}
}
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 ofPayment.CustomerId
doesn't matchint
type ofCustomer.Id
so EF need your hint about this.
– vasily.sib
Nov 8 at 17:31
add a comment |
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 ofPayment.CustomerId
doesn't matchint
type ofCustomer.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
add a comment |
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; }
}
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
add a comment |
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; }
}
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
add a comment |
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; }
}
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; }
}
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
add a comment |
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
add a comment |
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);
"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
add a comment |
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);
"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
add a comment |
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);
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);
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
add a comment |
"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
add a comment |
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!
add a comment |
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!
add a comment |
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!
I dropped my local database and re-created it with "update-database". That solved my problem. Thanks everybody for their time and effort!
answered Nov 12 at 14:58
Richard
62
62
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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