Sajid Hussain

Sajid Hussain

  • 1.2k
  • 514
  • 100.8k

How to show ddl in mvc VM

Dec 17 2017 4:03 PM
I have following two tables.
  1. CREATE TABLE [dbo].[CompanyProduct](  
  2.     [ID] [int] IDENTITY(1,1) NOT NULL,  
  3.     [StockName] [nvarchar](50) NOT NULL,  
  4.     [CompanyID] [intNOT NULL,  
  5.     [CreatedDate] [datetime] NOT NULL,  
  6.     [CreatedByID] [intNOT NULL,  
  7.     [StockDetail] [nvarchar](100) NULL,  
  8.  CONSTRAINT [PK_CompanyProduct] PRIMARY KEY CLUSTERED   
  9. (  
  10.     [ID] ASC  
  11. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]  
  12. ON [PRIMARY]  
  13.   
  14. GO  
  15.   
  16. ALTER TABLE [dbo].[CompanyProduct]  WITH CHECK ADD  CONSTRAINT [FK_CompanyProduct_TPSCompany] FOREIGN KEY([CompanyID])  
  17. REFERENCES [dbo].[TPSCompany] ([ID])  
  18. GO  
  19.   
  20. ALTER TABLE [dbo].[CompanyProduct] CHECK CONSTRAINT [FK_CompanyProduct_TPSCompany]  
  21. GO  
and
 
  1. CREATE TABLE [dbo].[TPSCompany](  
  2.     [ID] [int] IDENTITY(1,1) NOT NULL,  
  3.     [CompanyName] [nvarchar](100) NOT NULL,  
  4.     [ContactNumber] [nvarchar](50) NOT NULL,  
  5.     [DepartmentID] [tinyint] NOT NULL,  
  6.     [CreatedDate] [datetime] NOT NULL,  
  7.     [CreatedByID] [intNOT NULL,  
  8.  CONSTRAINT [PK_TPSCompanies] PRIMARY KEY CLUSTERED   
  9. (  
  10.     [ID] ASC  
  11. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]  
  12. ON [PRIMARY]  
  13.   
  14. GO  
  15.   
  16. ALTER TABLE [dbo].[TPSCompany]  WITH CHECK ADD  CONSTRAINT [FK_TPSCompanies_Department] FOREIGN KEY([DepartmentID])  
  17. REFERENCES [dbo].[Department] ([ID])  
  18. GO  
  19.   
  20. ALTER TABLE [dbo].[TPSCompany] CHECK CONSTRAINT [FK_TPSCompanies_Department]  
  21. GO  
 and using DB first approach , I have created classes of these tables.
 
for my use I have created a viewmodel with followings code
  1. public class CompanyProductVM  
  2.     {  
  3.          
  4.         public int ID { getset; }  
  5.   
  6.         [Required]  
  7.         [Display(Name = "Stock Name")]  
  8.         [DataType(DataType.Text)]  
  9.   
  10.         public string StockName { getset; }  
  11.           
  12.         public int CompanyID { getset; }  
  13.         [Required]  
  14.         [Display(Name = "Created Date")]  
  15.         [DataType(DataType.DateTime)]  
  16.   
  17.         public System.DateTime CreatedDate { getset; }  
  18.   
  19.   
  20.         public int CreatedByID { getset; }  
  21.         [Required]  
  22.         [Display(Name = "Stock Detail")]  
  23.         [DataType(DataType.Text)]  
  24.   
  25.   
  26.         public string StockDetail { getset; }  
  27.   
  28.         public virtual TPSCompany TPSCompany { getset; }  
  29.     }  
and I have CompanyProduct.cs as follow.
 
  1. public partial class CompanyProduct  
  2.    {  
  3.        public int ID { getset; }  
  4.        public string StockName { getset; }  
  5.        public int CompanyID { getset; }  
  6.        public System.DateTime CreatedDate { getset; }  
  7.        public int CreatedByID { getset; }  
  8.        public string StockDetail { getset; }  
  9.      
  10.        public virtual TPSCompany TPSCompany { getset; }  
  11.    }  
when I tried to generate Controller using scaffolding with EF.I am unable to get ddl for company as I want to select company agaisnt each company.this simpily create Controller.and if i want to change TPSCOMPANY to IEnum or collection then it send error that unable to get metadata for the application. 
 

Answers (3)