eusataf
eusataf

Reputation: 897

How to add a table to the model?

Used by:
- VS 2017;
- Win 7x64;
- MS Sql Server 2012;
- EntityFramework - 6.2.0;

  Model enter image description here  

Table "tbl_xx"  

    CREATE TABLE [dbo]. [Tbl_xx] (
       [Field] int NULL
    )
    ON [PRIMARY]
    GO
     
    ALTER TABLE [dbo]. [Tbl_xx] SET (LOCK_ESCALATION = TABLE
)

 
I try to add a table to the model  enter image description here  enter image description here enter image description here

Table not added.

Questions
1. Am I doing everything correctly to add a table to the model?
2. How to add a table to the model?

Update_1
I closed the opened VS.
No result.

Update_2
Added a key to the table. The table began to be added

 CREATE TABLE [dbo].[tbl_xx] (
      [Field] int  NULL,
      [id] int  NOT NULL,
      CONSTRAINT [PK__tbl_xx__3213E83FEB4094A9] PRIMARY KEY CLUSTERED ([id])
    WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)  
    ON [PRIMARY]
    )  
    ON [PRIMARY]
    GO

    ALTER TABLE [dbo].[tbl_xx] SET (LOCK_ESCALATION = TABLE)

Upvotes: 0

Views: 271

Answers (1)

Marty
Marty

Reputation: 545

For a class to be generated by EntityFramework the table/view needs to have a primary key column. You did already find this out. Also if you would investigate the XML of the .edmx file you would notice a commented section informing you, that EF couldn't identify a primary key for that table and therefore didn't generate anything.

When using EF I would consider a best practice for all tables to have IDENTITY columns with a PRIMARY KEY specified on them.

For the views it might be a bit more complicated, but you can still adjust the XML of the .edmx file to specify the key columns.

I would also suggest explicitly naming all DB keys (primary and foreign) and also all constraints for future maintenance and portability - the generated names will be different on different DBs and this could lead to complications later.

Upvotes: 2

Related Questions