unable to set other specified column as primary key instead of Id column to table using entity framework code first

Neo

I have two table Product and Order

public class Product
{
 string name;
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public Guid  productId { get; set; } // i want this to be primary key instead default Id
}

public class Order
{
 string name;
 [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public Guid  orderId { get; set; } // i want this to be primary key instead default Id and also want to add productId inside this table as foreign key
}

I have used following code to use code first.

DatabaseContext.cs

  class DatabaseContext : DbContext
    {
        public DbSet<Product> Products { get; set; }
        public DbSet<Order> Orders { get; set; }
    }

Program.cs

 static void Main(string[] args)
        {
            Database.SetInitializer(new DropCreateDatabaseAlways<DatabaseContext>());

            using (var context = new DatabaseContext())
            {
                context.Product.Add(new Product() {  Name = "mytest" });
                context.SaveChanges();
            }

            Console.WriteLine("Database Created!!!");
            Console.ReadKey();
        }

getting exception

Additional information: Unable to determine composite primary key ordering for type '.Customer'. Use the ColumnAttribute (see http://go.microsoft.com/fwlink/?LinkId=386388) or the HasKey method (see http://go.microsoft.com/fwlink/?LinkId=386387) to specify an order for composite primary keys.
Pawel

EF supports only properties so you should change your fields to properties. When you do that use {class Name} + Id as the property name and it will be picked up as the key property by convention. Alternatively you can use the [Key] attribute on a property to let EF know it should be the key property. You can find more about EF attributes here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Code First Migration - Entity Framework - unable to add column to the table

From Dev

Entity Framework Code First - Invalid column ID

From Dev

Create a complex Primary Key using Entity Framework code-first

From Dev

Entity Framework Self Referencing Using Non-Primary Key Column

From Dev

Violation of primary key Entity Framework Code First

From Dev

Autogenerate GUID column that is not a primary key in Entity Framework

From Dev

Entity Framework Primary Key column not auto incrementing

From Dev

Entity Framework Code first adds unwanted foreign key column

From Dev

Entity Framework not using my foreign key but generating a new column instead

From Dev

Adding a primary key for Entity Framework to an existing column in a View based on a table where every column is Allow Null

From Dev

Entity Framework code first, set column name on a Boolean property

From Dev

Add a column to a many to many relation table code first entity framework

From Dev

Entity mapping using a non primary key column

From Dev

Entity Framework composite key definition with column ordinal <> primary key ordinal

From Dev

primary key constraint updating many-to-many self referential table in entity framework code-first

From Dev

Join table in Entity Framework and display other column

From Dev

create text column with Entity Framework Code First

From Dev

Entity Framework Code First Migration - drop column

From Dev

entity framework Invalid column name Id (two foreign keys from the same primary table)

From Dev

Save detached object graph using Entity Framework code first causes Primary Key violation

From Dev

Save detached object graph using Entity Framework code first causes Primary Key violation

From Dev

Entity Framework code-first: querying a view with no primary key

From Dev

Entity framework code first cant create primary and foreign key relationship

From Dev

Entity Framework 5 and Code first - unable to add record to a table that has relation with other table(s)

From Dev

How to prevent to change other column values into table while updating single column using Entity Framework?

From Dev

How to prevent to change other column values into table while updating single column using Entity Framework?

From Dev

one to many relation with column other than Id (Primary Key)

From Dev

Retrieve primary key value of table when search for values in other column

From Dev

In Entity Framework code first, to map a navigation property do I also need to map the foreign key column

Related Related

  1. 1

    Code First Migration - Entity Framework - unable to add column to the table

  2. 2

    Entity Framework Code First - Invalid column ID

  3. 3

    Create a complex Primary Key using Entity Framework code-first

  4. 4

    Entity Framework Self Referencing Using Non-Primary Key Column

  5. 5

    Violation of primary key Entity Framework Code First

  6. 6

    Autogenerate GUID column that is not a primary key in Entity Framework

  7. 7

    Entity Framework Primary Key column not auto incrementing

  8. 8

    Entity Framework Code first adds unwanted foreign key column

  9. 9

    Entity Framework not using my foreign key but generating a new column instead

  10. 10

    Adding a primary key for Entity Framework to an existing column in a View based on a table where every column is Allow Null

  11. 11

    Entity Framework code first, set column name on a Boolean property

  12. 12

    Add a column to a many to many relation table code first entity framework

  13. 13

    Entity mapping using a non primary key column

  14. 14

    Entity Framework composite key definition with column ordinal <> primary key ordinal

  15. 15

    primary key constraint updating many-to-many self referential table in entity framework code-first

  16. 16

    Join table in Entity Framework and display other column

  17. 17

    create text column with Entity Framework Code First

  18. 18

    Entity Framework Code First Migration - drop column

  19. 19

    entity framework Invalid column name Id (two foreign keys from the same primary table)

  20. 20

    Save detached object graph using Entity Framework code first causes Primary Key violation

  21. 21

    Save detached object graph using Entity Framework code first causes Primary Key violation

  22. 22

    Entity Framework code-first: querying a view with no primary key

  23. 23

    Entity framework code first cant create primary and foreign key relationship

  24. 24

    Entity Framework 5 and Code first - unable to add record to a table that has relation with other table(s)

  25. 25

    How to prevent to change other column values into table while updating single column using Entity Framework?

  26. 26

    How to prevent to change other column values into table while updating single column using Entity Framework?

  27. 27

    one to many relation with column other than Id (Primary Key)

  28. 28

    Retrieve primary key value of table when search for values in other column

  29. 29

    In Entity Framework code first, to map a navigation property do I also need to map the foreign key column

HotTag

Archive