One to one relation in EF Core using Fluent API [duplicate]
This question already has an answer here:
One-to-Zero relationship with Entity Framework Core 2.0
1 answer
I am using EF Core 2.1
How to map one-to-one relationship in EF Core. I have Customer & Course domain entity where one customer will have one Course.
This is my how my Customer & CoursePOCO looks like.
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Course
{
public int Id { get; set; }
public string CouseName { get; set; }
public virtual Customer Customer { get; set; }
}
I am using FluentAPI.
public class CourseConfiguration : IEntityTypeConfiguration<Course>
{
public void Configure(EntityTypeBuilder<Parent> builder)
{
builder.HasKey(x => x.Customer.Id) //not allowing -> throws error
//The properties expression 'x => Convert(x.Customer.Id, Object)' is not valid.
// The expression should represent a simple property access: 't => t.MyProperty'.
// When specifying multiple properties use an anonymous type: 't => new { t.MyProperty1, t.MyProperty2 }'.
// Parameter name: propertyAccessExpression
}
}
Since it's one to one relation, I don't want to create an extra key in Contact (FK -CustomerId),
Primary requirement:-
I want to define Id (in Course) as PK + FK & in this relation Customer is parent entity.
Like if I was Configuration based Migration, I would do as follows:-
public class Course
{
[Key]
[ForeignKey("Customer")]
public int Id { get; set; }
public string Name { get; set; }
public virtual Customer Customer { get; set; }
}
same thing I would like to do using Fluent API in EF Core??
Thanks!!
c# asp.net-core entity-framework-core ef-migrations ef-core-2.1
marked as duplicate by Ivan Stoev
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 31 '18 at 12:06
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.
add a comment |
This question already has an answer here:
One-to-Zero relationship with Entity Framework Core 2.0
1 answer
I am using EF Core 2.1
How to map one-to-one relationship in EF Core. I have Customer & Course domain entity where one customer will have one Course.
This is my how my Customer & CoursePOCO looks like.
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Course
{
public int Id { get; set; }
public string CouseName { get; set; }
public virtual Customer Customer { get; set; }
}
I am using FluentAPI.
public class CourseConfiguration : IEntityTypeConfiguration<Course>
{
public void Configure(EntityTypeBuilder<Parent> builder)
{
builder.HasKey(x => x.Customer.Id) //not allowing -> throws error
//The properties expression 'x => Convert(x.Customer.Id, Object)' is not valid.
// The expression should represent a simple property access: 't => t.MyProperty'.
// When specifying multiple properties use an anonymous type: 't => new { t.MyProperty1, t.MyProperty2 }'.
// Parameter name: propertyAccessExpression
}
}
Since it's one to one relation, I don't want to create an extra key in Contact (FK -CustomerId),
Primary requirement:-
I want to define Id (in Course) as PK + FK & in this relation Customer is parent entity.
Like if I was Configuration based Migration, I would do as follows:-
public class Course
{
[Key]
[ForeignKey("Customer")]
public int Id { get; set; }
public string Name { get; set; }
public virtual Customer Customer { get; set; }
}
same thing I would like to do using Fluent API in EF Core??
Thanks!!
c# asp.net-core entity-framework-core ef-migrations ef-core-2.1
marked as duplicate by Ivan Stoev
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 31 '18 at 12:06
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.
Just curious on why do you want to have the customer fk as Id on Course? Course is something generic.. instead I think a better model would be a Customer have a course, don't you think? Your model become simpler this way, and, if later you decide to tie you Course with something else, you are safe.
– jpgrassi
Dec 31 '18 at 11:09
add a comment |
This question already has an answer here:
One-to-Zero relationship with Entity Framework Core 2.0
1 answer
I am using EF Core 2.1
How to map one-to-one relationship in EF Core. I have Customer & Course domain entity where one customer will have one Course.
This is my how my Customer & CoursePOCO looks like.
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Course
{
public int Id { get; set; }
public string CouseName { get; set; }
public virtual Customer Customer { get; set; }
}
I am using FluentAPI.
public class CourseConfiguration : IEntityTypeConfiguration<Course>
{
public void Configure(EntityTypeBuilder<Parent> builder)
{
builder.HasKey(x => x.Customer.Id) //not allowing -> throws error
//The properties expression 'x => Convert(x.Customer.Id, Object)' is not valid.
// The expression should represent a simple property access: 't => t.MyProperty'.
// When specifying multiple properties use an anonymous type: 't => new { t.MyProperty1, t.MyProperty2 }'.
// Parameter name: propertyAccessExpression
}
}
Since it's one to one relation, I don't want to create an extra key in Contact (FK -CustomerId),
Primary requirement:-
I want to define Id (in Course) as PK + FK & in this relation Customer is parent entity.
Like if I was Configuration based Migration, I would do as follows:-
public class Course
{
[Key]
[ForeignKey("Customer")]
public int Id { get; set; }
public string Name { get; set; }
public virtual Customer Customer { get; set; }
}
same thing I would like to do using Fluent API in EF Core??
Thanks!!
c# asp.net-core entity-framework-core ef-migrations ef-core-2.1
This question already has an answer here:
One-to-Zero relationship with Entity Framework Core 2.0
1 answer
I am using EF Core 2.1
How to map one-to-one relationship in EF Core. I have Customer & Course domain entity where one customer will have one Course.
This is my how my Customer & CoursePOCO looks like.
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Course
{
public int Id { get; set; }
public string CouseName { get; set; }
public virtual Customer Customer { get; set; }
}
I am using FluentAPI.
public class CourseConfiguration : IEntityTypeConfiguration<Course>
{
public void Configure(EntityTypeBuilder<Parent> builder)
{
builder.HasKey(x => x.Customer.Id) //not allowing -> throws error
//The properties expression 'x => Convert(x.Customer.Id, Object)' is not valid.
// The expression should represent a simple property access: 't => t.MyProperty'.
// When specifying multiple properties use an anonymous type: 't => new { t.MyProperty1, t.MyProperty2 }'.
// Parameter name: propertyAccessExpression
}
}
Since it's one to one relation, I don't want to create an extra key in Contact (FK -CustomerId),
Primary requirement:-
I want to define Id (in Course) as PK + FK & in this relation Customer is parent entity.
Like if I was Configuration based Migration, I would do as follows:-
public class Course
{
[Key]
[ForeignKey("Customer")]
public int Id { get; set; }
public string Name { get; set; }
public virtual Customer Customer { get; set; }
}
same thing I would like to do using Fluent API in EF Core??
Thanks!!
This question already has an answer here:
One-to-Zero relationship with Entity Framework Core 2.0
1 answer
c# asp.net-core entity-framework-core ef-migrations ef-core-2.1
c# asp.net-core entity-framework-core ef-migrations ef-core-2.1
edited Dec 31 '18 at 12:07
Kgn-web
asked Dec 31 '18 at 10:53
Kgn-webKgn-web
1,99462660
1,99462660
marked as duplicate by Ivan Stoev
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 31 '18 at 12:06
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 Ivan Stoev
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 31 '18 at 12:06
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.
Just curious on why do you want to have the customer fk as Id on Course? Course is something generic.. instead I think a better model would be a Customer have a course, don't you think? Your model become simpler this way, and, if later you decide to tie you Course with something else, you are safe.
– jpgrassi
Dec 31 '18 at 11:09
add a comment |
Just curious on why do you want to have the customer fk as Id on Course? Course is something generic.. instead I think a better model would be a Customer have a course, don't you think? Your model become simpler this way, and, if later you decide to tie you Course with something else, you are safe.
– jpgrassi
Dec 31 '18 at 11:09
Just curious on why do you want to have the customer fk as Id on Course? Course is something generic.. instead I think a better model would be a Customer have a course, don't you think? Your model become simpler this way, and, if later you decide to tie you Course with something else, you are safe.
– jpgrassi
Dec 31 '18 at 11:09
Just curious on why do you want to have the customer fk as Id on Course? Course is something generic.. instead I think a better model would be a Customer have a course, don't you think? Your model become simpler this way, and, if later you decide to tie you Course with something else, you are safe.
– jpgrassi
Dec 31 '18 at 11:09
add a comment |
2 Answers
2
active
oldest
votes
As other answers point out, the key point is to use use the HasForeignKey<>() method to configure a foreign key.
But be careful that the foreign key should be set on the dependent entity, instead of the principal entity.
How to in details:
Add a navigation property to Course for Customer
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public Course Course {get;set;}
}
and now set the Course.Id as a FK that references the Customer.Id
public class AppDbContext : DbContext
{
public AppDbContext (DbContextOptions<AppDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<Customer>(entity=>
{
entity.HasOne(customer=>customer.Course)
.WithOne(course=> course.Customer)
.HasForeignKey<Course>(course=>course.Id);
});
}
public DbSet<App.Models.Customer> Customer { get; set; }
public DbSet<App.Models.Course> Courses{ get; set; }
}
the generated sql scripts is :
CREATE TABLE [Customer] (
[Id] int NOT NULL IDENTITY,
[Name] nvarchar(max) NULL,
CONSTRAINT [PK_Customer] PRIMARY KEY ([Id])
);
GO
CREATE TABLE [Courses] (
[Id] int NOT NULL,
[CouseName] nvarchar(max) NULL,
CONSTRAINT [PK_Courses] PRIMARY KEY ([Id]),
CONSTRAINT [FK_Courses_Customer_Id] FOREIGN KEY ([Id]) REFERENCES [Customer] ([Id]) ON DELETE CASCADE
);
GO
Thanks for highlight the principal entity point, but still the primary requirement is unaddressed. I don't want to have Id & Customer as PK & FK respectively instead Id in course should server as PK & FK both
– Kgn-web
Dec 31 '18 at 12:05
@Kgn-web I don't create a PK & FK respectively. the Course.Id serves as PK and FK at the same time.
– itminus
Dec 31 '18 at 12:06
@Kgn-web you could inspect the sql scripts above (generated bydotnet ef migrations script) to confirm thatCourses.Idserves as PK and FK at the same time.
– itminus
Dec 31 '18 at 12:11
Checking in progress,Please allow me some minutes Thanks
– Kgn-web
Dec 31 '18 at 12:12
cool. Yes. your post addressed my requirement.. Thanks
– Kgn-web
Dec 31 '18 at 12:18
add a comment |
Something similar to below should help. HasOne method is chained with WithOne would help to establish one-to-one relationship :
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Course
{
public int Id { get; set; }
public string CourseName { get; set; }
public int CustomerId {get;set;}
public virtual Customer Customer { get; set; }
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Course>()
.HasOne(a => a.Customer)
.WithOne(b => b.Course)
.HasForeignKey<Course>(b => b.CustomerId);
}
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
As other answers point out, the key point is to use use the HasForeignKey<>() method to configure a foreign key.
But be careful that the foreign key should be set on the dependent entity, instead of the principal entity.
How to in details:
Add a navigation property to Course for Customer
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public Course Course {get;set;}
}
and now set the Course.Id as a FK that references the Customer.Id
public class AppDbContext : DbContext
{
public AppDbContext (DbContextOptions<AppDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<Customer>(entity=>
{
entity.HasOne(customer=>customer.Course)
.WithOne(course=> course.Customer)
.HasForeignKey<Course>(course=>course.Id);
});
}
public DbSet<App.Models.Customer> Customer { get; set; }
public DbSet<App.Models.Course> Courses{ get; set; }
}
the generated sql scripts is :
CREATE TABLE [Customer] (
[Id] int NOT NULL IDENTITY,
[Name] nvarchar(max) NULL,
CONSTRAINT [PK_Customer] PRIMARY KEY ([Id])
);
GO
CREATE TABLE [Courses] (
[Id] int NOT NULL,
[CouseName] nvarchar(max) NULL,
CONSTRAINT [PK_Courses] PRIMARY KEY ([Id]),
CONSTRAINT [FK_Courses_Customer_Id] FOREIGN KEY ([Id]) REFERENCES [Customer] ([Id]) ON DELETE CASCADE
);
GO
Thanks for highlight the principal entity point, but still the primary requirement is unaddressed. I don't want to have Id & Customer as PK & FK respectively instead Id in course should server as PK & FK both
– Kgn-web
Dec 31 '18 at 12:05
@Kgn-web I don't create a PK & FK respectively. the Course.Id serves as PK and FK at the same time.
– itminus
Dec 31 '18 at 12:06
@Kgn-web you could inspect the sql scripts above (generated bydotnet ef migrations script) to confirm thatCourses.Idserves as PK and FK at the same time.
– itminus
Dec 31 '18 at 12:11
Checking in progress,Please allow me some minutes Thanks
– Kgn-web
Dec 31 '18 at 12:12
cool. Yes. your post addressed my requirement.. Thanks
– Kgn-web
Dec 31 '18 at 12:18
add a comment |
As other answers point out, the key point is to use use the HasForeignKey<>() method to configure a foreign key.
But be careful that the foreign key should be set on the dependent entity, instead of the principal entity.
How to in details:
Add a navigation property to Course for Customer
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public Course Course {get;set;}
}
and now set the Course.Id as a FK that references the Customer.Id
public class AppDbContext : DbContext
{
public AppDbContext (DbContextOptions<AppDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<Customer>(entity=>
{
entity.HasOne(customer=>customer.Course)
.WithOne(course=> course.Customer)
.HasForeignKey<Course>(course=>course.Id);
});
}
public DbSet<App.Models.Customer> Customer { get; set; }
public DbSet<App.Models.Course> Courses{ get; set; }
}
the generated sql scripts is :
CREATE TABLE [Customer] (
[Id] int NOT NULL IDENTITY,
[Name] nvarchar(max) NULL,
CONSTRAINT [PK_Customer] PRIMARY KEY ([Id])
);
GO
CREATE TABLE [Courses] (
[Id] int NOT NULL,
[CouseName] nvarchar(max) NULL,
CONSTRAINT [PK_Courses] PRIMARY KEY ([Id]),
CONSTRAINT [FK_Courses_Customer_Id] FOREIGN KEY ([Id]) REFERENCES [Customer] ([Id]) ON DELETE CASCADE
);
GO
Thanks for highlight the principal entity point, but still the primary requirement is unaddressed. I don't want to have Id & Customer as PK & FK respectively instead Id in course should server as PK & FK both
– Kgn-web
Dec 31 '18 at 12:05
@Kgn-web I don't create a PK & FK respectively. the Course.Id serves as PK and FK at the same time.
– itminus
Dec 31 '18 at 12:06
@Kgn-web you could inspect the sql scripts above (generated bydotnet ef migrations script) to confirm thatCourses.Idserves as PK and FK at the same time.
– itminus
Dec 31 '18 at 12:11
Checking in progress,Please allow me some minutes Thanks
– Kgn-web
Dec 31 '18 at 12:12
cool. Yes. your post addressed my requirement.. Thanks
– Kgn-web
Dec 31 '18 at 12:18
add a comment |
As other answers point out, the key point is to use use the HasForeignKey<>() method to configure a foreign key.
But be careful that the foreign key should be set on the dependent entity, instead of the principal entity.
How to in details:
Add a navigation property to Course for Customer
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public Course Course {get;set;}
}
and now set the Course.Id as a FK that references the Customer.Id
public class AppDbContext : DbContext
{
public AppDbContext (DbContextOptions<AppDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<Customer>(entity=>
{
entity.HasOne(customer=>customer.Course)
.WithOne(course=> course.Customer)
.HasForeignKey<Course>(course=>course.Id);
});
}
public DbSet<App.Models.Customer> Customer { get; set; }
public DbSet<App.Models.Course> Courses{ get; set; }
}
the generated sql scripts is :
CREATE TABLE [Customer] (
[Id] int NOT NULL IDENTITY,
[Name] nvarchar(max) NULL,
CONSTRAINT [PK_Customer] PRIMARY KEY ([Id])
);
GO
CREATE TABLE [Courses] (
[Id] int NOT NULL,
[CouseName] nvarchar(max) NULL,
CONSTRAINT [PK_Courses] PRIMARY KEY ([Id]),
CONSTRAINT [FK_Courses_Customer_Id] FOREIGN KEY ([Id]) REFERENCES [Customer] ([Id]) ON DELETE CASCADE
);
GO
As other answers point out, the key point is to use use the HasForeignKey<>() method to configure a foreign key.
But be careful that the foreign key should be set on the dependent entity, instead of the principal entity.
How to in details:
Add a navigation property to Course for Customer
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public Course Course {get;set;}
}
and now set the Course.Id as a FK that references the Customer.Id
public class AppDbContext : DbContext
{
public AppDbContext (DbContextOptions<AppDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<Customer>(entity=>
{
entity.HasOne(customer=>customer.Course)
.WithOne(course=> course.Customer)
.HasForeignKey<Course>(course=>course.Id);
});
}
public DbSet<App.Models.Customer> Customer { get; set; }
public DbSet<App.Models.Course> Courses{ get; set; }
}
the generated sql scripts is :
CREATE TABLE [Customer] (
[Id] int NOT NULL IDENTITY,
[Name] nvarchar(max) NULL,
CONSTRAINT [PK_Customer] PRIMARY KEY ([Id])
);
GO
CREATE TABLE [Courses] (
[Id] int NOT NULL,
[CouseName] nvarchar(max) NULL,
CONSTRAINT [PK_Courses] PRIMARY KEY ([Id]),
CONSTRAINT [FK_Courses_Customer_Id] FOREIGN KEY ([Id]) REFERENCES [Customer] ([Id]) ON DELETE CASCADE
);
GO
answered Dec 31 '18 at 12:00
itminusitminus
3,4961321
3,4961321
Thanks for highlight the principal entity point, but still the primary requirement is unaddressed. I don't want to have Id & Customer as PK & FK respectively instead Id in course should server as PK & FK both
– Kgn-web
Dec 31 '18 at 12:05
@Kgn-web I don't create a PK & FK respectively. the Course.Id serves as PK and FK at the same time.
– itminus
Dec 31 '18 at 12:06
@Kgn-web you could inspect the sql scripts above (generated bydotnet ef migrations script) to confirm thatCourses.Idserves as PK and FK at the same time.
– itminus
Dec 31 '18 at 12:11
Checking in progress,Please allow me some minutes Thanks
– Kgn-web
Dec 31 '18 at 12:12
cool. Yes. your post addressed my requirement.. Thanks
– Kgn-web
Dec 31 '18 at 12:18
add a comment |
Thanks for highlight the principal entity point, but still the primary requirement is unaddressed. I don't want to have Id & Customer as PK & FK respectively instead Id in course should server as PK & FK both
– Kgn-web
Dec 31 '18 at 12:05
@Kgn-web I don't create a PK & FK respectively. the Course.Id serves as PK and FK at the same time.
– itminus
Dec 31 '18 at 12:06
@Kgn-web you could inspect the sql scripts above (generated bydotnet ef migrations script) to confirm thatCourses.Idserves as PK and FK at the same time.
– itminus
Dec 31 '18 at 12:11
Checking in progress,Please allow me some minutes Thanks
– Kgn-web
Dec 31 '18 at 12:12
cool. Yes. your post addressed my requirement.. Thanks
– Kgn-web
Dec 31 '18 at 12:18
Thanks for highlight the principal entity point, but still the primary requirement is unaddressed. I don't want to have Id & Customer as PK & FK respectively instead Id in course should server as PK & FK both
– Kgn-web
Dec 31 '18 at 12:05
Thanks for highlight the principal entity point, but still the primary requirement is unaddressed. I don't want to have Id & Customer as PK & FK respectively instead Id in course should server as PK & FK both
– Kgn-web
Dec 31 '18 at 12:05
@Kgn-web I don't create a PK & FK respectively. the Course.Id serves as PK and FK at the same time.
– itminus
Dec 31 '18 at 12:06
@Kgn-web I don't create a PK & FK respectively. the Course.Id serves as PK and FK at the same time.
– itminus
Dec 31 '18 at 12:06
@Kgn-web you could inspect the sql scripts above (generated by
dotnet ef migrations script) to confirm that Courses.Id serves as PK and FK at the same time.– itminus
Dec 31 '18 at 12:11
@Kgn-web you could inspect the sql scripts above (generated by
dotnet ef migrations script) to confirm that Courses.Id serves as PK and FK at the same time.– itminus
Dec 31 '18 at 12:11
Checking in progress,Please allow me some minutes Thanks
– Kgn-web
Dec 31 '18 at 12:12
Checking in progress,Please allow me some minutes Thanks
– Kgn-web
Dec 31 '18 at 12:12
cool. Yes. your post addressed my requirement.. Thanks
– Kgn-web
Dec 31 '18 at 12:18
cool. Yes. your post addressed my requirement.. Thanks
– Kgn-web
Dec 31 '18 at 12:18
add a comment |
Something similar to below should help. HasOne method is chained with WithOne would help to establish one-to-one relationship :
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Course
{
public int Id { get; set; }
public string CourseName { get; set; }
public int CustomerId {get;set;}
public virtual Customer Customer { get; set; }
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Course>()
.HasOne(a => a.Customer)
.WithOne(b => b.Course)
.HasForeignKey<Course>(b => b.CustomerId);
}
add a comment |
Something similar to below should help. HasOne method is chained with WithOne would help to establish one-to-one relationship :
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Course
{
public int Id { get; set; }
public string CourseName { get; set; }
public int CustomerId {get;set;}
public virtual Customer Customer { get; set; }
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Course>()
.HasOne(a => a.Customer)
.WithOne(b => b.Course)
.HasForeignKey<Course>(b => b.CustomerId);
}
add a comment |
Something similar to below should help. HasOne method is chained with WithOne would help to establish one-to-one relationship :
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Course
{
public int Id { get; set; }
public string CourseName { get; set; }
public int CustomerId {get;set;}
public virtual Customer Customer { get; set; }
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Course>()
.HasOne(a => a.Customer)
.WithOne(b => b.Course)
.HasForeignKey<Course>(b => b.CustomerId);
}
Something similar to below should help. HasOne method is chained with WithOne would help to establish one-to-one relationship :
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Course
{
public int Id { get; set; }
public string CourseName { get; set; }
public int CustomerId {get;set;}
public virtual Customer Customer { get; set; }
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Course>()
.HasOne(a => a.Customer)
.WithOne(b => b.Course)
.HasForeignKey<Course>(b => b.CustomerId);
}
answered Dec 31 '18 at 11:29
Manoj ChoudhariManoj Choudhari
1,427516
1,427516
add a comment |
add a comment |
Just curious on why do you want to have the customer fk as Id on Course? Course is something generic.. instead I think a better model would be a Customer have a course, don't you think? Your model become simpler this way, and, if later you decide to tie you Course with something else, you are safe.
– jpgrassi
Dec 31 '18 at 11:09