Complex Type requires primary key
up vote
0
down vote
favorite
I have an object that contains an attribute with the type of another object, which I want to treat as Complex Type.
public class Location : IModule
{
public string Id { get; set; }
public Coordinate Coordinate { get; set; }
}
[ComplexType]
public class Coordinate
{
public string Latitude { get; set; }
public string Longitude { get; set; }
}
While adding a migration, I ran into the problem that a primary key is required (exactly what I want to prevent).
The entity type Coordinate
requires a primary key to be defined.
EDIT
For performance reasons I want the properties being stored as Coordinate_Latitude
and Coordinate_Longitute
instead of having a reference to another table.
c# .net entity-framework primary-key
|
show 2 more comments
up vote
0
down vote
favorite
I have an object that contains an attribute with the type of another object, which I want to treat as Complex Type.
public class Location : IModule
{
public string Id { get; set; }
public Coordinate Coordinate { get; set; }
}
[ComplexType]
public class Coordinate
{
public string Latitude { get; set; }
public string Longitude { get; set; }
}
While adding a migration, I ran into the problem that a primary key is required (exactly what I want to prevent).
The entity type Coordinate
requires a primary key to be defined.
EDIT
For performance reasons I want the properties being stored as Coordinate_Latitude
and Coordinate_Longitute
instead of having a reference to another table.
c# .net entity-framework primary-key
1
exactly what I want to prevent, Why?
– S.Akbari
Nov 21 at 16:49
see edit - for performance reasons
– David
Nov 21 at 16:51
1
adding a primary (clustered) key will actually improve performance significantly
– DaniDev
Nov 21 at 17:54
I do have a primary key on Location, I want to store the attributes of Coordinate within the same table as Location. So I disagree, saving these attributes in the same table will be more efficient than storing them in a different table referencing them. Adding a key to Coordinate would create another table which would lead to more IOs for every single query.
– David
Nov 21 at 22:03
I'm asking about the Annotation ComplexType, which promises to store attributes in the same table.
– David
Nov 21 at 22:07
|
show 2 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have an object that contains an attribute with the type of another object, which I want to treat as Complex Type.
public class Location : IModule
{
public string Id { get; set; }
public Coordinate Coordinate { get; set; }
}
[ComplexType]
public class Coordinate
{
public string Latitude { get; set; }
public string Longitude { get; set; }
}
While adding a migration, I ran into the problem that a primary key is required (exactly what I want to prevent).
The entity type Coordinate
requires a primary key to be defined.
EDIT
For performance reasons I want the properties being stored as Coordinate_Latitude
and Coordinate_Longitute
instead of having a reference to another table.
c# .net entity-framework primary-key
I have an object that contains an attribute with the type of another object, which I want to treat as Complex Type.
public class Location : IModule
{
public string Id { get; set; }
public Coordinate Coordinate { get; set; }
}
[ComplexType]
public class Coordinate
{
public string Latitude { get; set; }
public string Longitude { get; set; }
}
While adding a migration, I ran into the problem that a primary key is required (exactly what I want to prevent).
The entity type Coordinate
requires a primary key to be defined.
EDIT
For performance reasons I want the properties being stored as Coordinate_Latitude
and Coordinate_Longitute
instead of having a reference to another table.
c# .net entity-framework primary-key
c# .net entity-framework primary-key
edited Nov 21 at 18:35
S.Akbari
29.8k93471
29.8k93471
asked Nov 21 at 16:46
David
415
415
1
exactly what I want to prevent, Why?
– S.Akbari
Nov 21 at 16:49
see edit - for performance reasons
– David
Nov 21 at 16:51
1
adding a primary (clustered) key will actually improve performance significantly
– DaniDev
Nov 21 at 17:54
I do have a primary key on Location, I want to store the attributes of Coordinate within the same table as Location. So I disagree, saving these attributes in the same table will be more efficient than storing them in a different table referencing them. Adding a key to Coordinate would create another table which would lead to more IOs for every single query.
– David
Nov 21 at 22:03
I'm asking about the Annotation ComplexType, which promises to store attributes in the same table.
– David
Nov 21 at 22:07
|
show 2 more comments
1
exactly what I want to prevent, Why?
– S.Akbari
Nov 21 at 16:49
see edit - for performance reasons
– David
Nov 21 at 16:51
1
adding a primary (clustered) key will actually improve performance significantly
– DaniDev
Nov 21 at 17:54
I do have a primary key on Location, I want to store the attributes of Coordinate within the same table as Location. So I disagree, saving these attributes in the same table will be more efficient than storing them in a different table referencing them. Adding a key to Coordinate would create another table which would lead to more IOs for every single query.
– David
Nov 21 at 22:03
I'm asking about the Annotation ComplexType, which promises to store attributes in the same table.
– David
Nov 21 at 22:07
1
1
exactly what I want to prevent, Why?
– S.Akbari
Nov 21 at 16:49
exactly what I want to prevent, Why?
– S.Akbari
Nov 21 at 16:49
see edit - for performance reasons
– David
Nov 21 at 16:51
see edit - for performance reasons
– David
Nov 21 at 16:51
1
1
adding a primary (clustered) key will actually improve performance significantly
– DaniDev
Nov 21 at 17:54
adding a primary (clustered) key will actually improve performance significantly
– DaniDev
Nov 21 at 17:54
I do have a primary key on Location, I want to store the attributes of Coordinate within the same table as Location. So I disagree, saving these attributes in the same table will be more efficient than storing them in a different table referencing them. Adding a key to Coordinate would create another table which would lead to more IOs for every single query.
– David
Nov 21 at 22:03
I do have a primary key on Location, I want to store the attributes of Coordinate within the same table as Location. So I disagree, saving these attributes in the same table will be more efficient than storing them in a different table referencing them. Adding a key to Coordinate would create another table which would lead to more IOs for every single query.
– David
Nov 21 at 22:03
I'm asking about the Annotation ComplexType, which promises to store attributes in the same table.
– David
Nov 21 at 22:07
I'm asking about the Annotation ComplexType, which promises to store attributes in the same table.
– David
Nov 21 at 22:07
|
show 2 more comments
2 Answers
2
active
oldest
votes
up vote
2
down vote
You need to define a key, to make it works. This is how the Entity Framework works, Entity Framework needs to know the key to keep track on the object when you make an update or delete operation. Just if you don't want to manually insert it, you can declare it as an identity column to auto increment it. Something like this:
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CoordinateKey { get; set; }
Or with Fluent-API:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Coordinate>().HasKey(u => u.CoordinateKey);
modelBuilder.Entity<Coordinate>().Property(c => c.CoordinateKey)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
}
Edit: It seems you want to treat the Coordinate
class as a not mapped class. You can use NotMapped
attribute. Have a look at the following question to know how:
Entity Framework code first: How to ignore classes
add a comment |
up vote
0
down vote
Based on this question (How do I implement a simple "complex type" in Entity Framework Core 2/C#?), I found the answer: Owned entity types do the trick.
public class Location : IModule
{
public string Id { get; set; }
public Coordinate Coordinate { get; set; }
}
[Owned]
public class Coordinate
{
public string Latitude { get; set; }
public string Longitude { get; set; }
}
This creates a table containt the attributes Id
, Coordinate_Latitued
, Coordinate_Longitude
.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
You need to define a key, to make it works. This is how the Entity Framework works, Entity Framework needs to know the key to keep track on the object when you make an update or delete operation. Just if you don't want to manually insert it, you can declare it as an identity column to auto increment it. Something like this:
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CoordinateKey { get; set; }
Or with Fluent-API:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Coordinate>().HasKey(u => u.CoordinateKey);
modelBuilder.Entity<Coordinate>().Property(c => c.CoordinateKey)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
}
Edit: It seems you want to treat the Coordinate
class as a not mapped class. You can use NotMapped
attribute. Have a look at the following question to know how:
Entity Framework code first: How to ignore classes
add a comment |
up vote
2
down vote
You need to define a key, to make it works. This is how the Entity Framework works, Entity Framework needs to know the key to keep track on the object when you make an update or delete operation. Just if you don't want to manually insert it, you can declare it as an identity column to auto increment it. Something like this:
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CoordinateKey { get; set; }
Or with Fluent-API:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Coordinate>().HasKey(u => u.CoordinateKey);
modelBuilder.Entity<Coordinate>().Property(c => c.CoordinateKey)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
}
Edit: It seems you want to treat the Coordinate
class as a not mapped class. You can use NotMapped
attribute. Have a look at the following question to know how:
Entity Framework code first: How to ignore classes
add a comment |
up vote
2
down vote
up vote
2
down vote
You need to define a key, to make it works. This is how the Entity Framework works, Entity Framework needs to know the key to keep track on the object when you make an update or delete operation. Just if you don't want to manually insert it, you can declare it as an identity column to auto increment it. Something like this:
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CoordinateKey { get; set; }
Or with Fluent-API:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Coordinate>().HasKey(u => u.CoordinateKey);
modelBuilder.Entity<Coordinate>().Property(c => c.CoordinateKey)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
}
Edit: It seems you want to treat the Coordinate
class as a not mapped class. You can use NotMapped
attribute. Have a look at the following question to know how:
Entity Framework code first: How to ignore classes
You need to define a key, to make it works. This is how the Entity Framework works, Entity Framework needs to know the key to keep track on the object when you make an update or delete operation. Just if you don't want to manually insert it, you can declare it as an identity column to auto increment it. Something like this:
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CoordinateKey { get; set; }
Or with Fluent-API:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Coordinate>().HasKey(u => u.CoordinateKey);
modelBuilder.Entity<Coordinate>().Property(c => c.CoordinateKey)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
}
Edit: It seems you want to treat the Coordinate
class as a not mapped class. You can use NotMapped
attribute. Have a look at the following question to know how:
Entity Framework code first: How to ignore classes
edited Nov 22 at 7:42
answered Nov 21 at 16:52
S.Akbari
29.8k93471
29.8k93471
add a comment |
add a comment |
up vote
0
down vote
Based on this question (How do I implement a simple "complex type" in Entity Framework Core 2/C#?), I found the answer: Owned entity types do the trick.
public class Location : IModule
{
public string Id { get; set; }
public Coordinate Coordinate { get; set; }
}
[Owned]
public class Coordinate
{
public string Latitude { get; set; }
public string Longitude { get; set; }
}
This creates a table containt the attributes Id
, Coordinate_Latitued
, Coordinate_Longitude
.
add a comment |
up vote
0
down vote
Based on this question (How do I implement a simple "complex type" in Entity Framework Core 2/C#?), I found the answer: Owned entity types do the trick.
public class Location : IModule
{
public string Id { get; set; }
public Coordinate Coordinate { get; set; }
}
[Owned]
public class Coordinate
{
public string Latitude { get; set; }
public string Longitude { get; set; }
}
This creates a table containt the attributes Id
, Coordinate_Latitued
, Coordinate_Longitude
.
add a comment |
up vote
0
down vote
up vote
0
down vote
Based on this question (How do I implement a simple "complex type" in Entity Framework Core 2/C#?), I found the answer: Owned entity types do the trick.
public class Location : IModule
{
public string Id { get; set; }
public Coordinate Coordinate { get; set; }
}
[Owned]
public class Coordinate
{
public string Latitude { get; set; }
public string Longitude { get; set; }
}
This creates a table containt the attributes Id
, Coordinate_Latitued
, Coordinate_Longitude
.
Based on this question (How do I implement a simple "complex type" in Entity Framework Core 2/C#?), I found the answer: Owned entity types do the trick.
public class Location : IModule
{
public string Id { get; set; }
public Coordinate Coordinate { get; set; }
}
[Owned]
public class Coordinate
{
public string Latitude { get; set; }
public string Longitude { get; set; }
}
This creates a table containt the attributes Id
, Coordinate_Latitued
, Coordinate_Longitude
.
answered Nov 22 at 12:30
David
415
415
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53416869%2fcomplex-type-requires-primary-key%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
1
exactly what I want to prevent, Why?
– S.Akbari
Nov 21 at 16:49
see edit - for performance reasons
– David
Nov 21 at 16:51
1
adding a primary (clustered) key will actually improve performance significantly
– DaniDev
Nov 21 at 17:54
I do have a primary key on Location, I want to store the attributes of Coordinate within the same table as Location. So I disagree, saving these attributes in the same table will be more efficient than storing them in a different table referencing them. Adding a key to Coordinate would create another table which would lead to more IOs for every single query.
– David
Nov 21 at 22:03
I'm asking about the Annotation ComplexType, which promises to store attributes in the same table.
– David
Nov 21 at 22:07