简体   繁体   中英

how I can do stored procedure custom complex type using class mapping

This my custom class. I want this using class custom create complex type.

I am using C#, ASP.NET MVC 4.5 and SQL Server 2012

Thank you

SQL Server table name : CompanyModel

public class CompanyModel
{
    public int CompanyID { get; set; }        
    public string SubDomain { get; set; }     
    public string CompanyName { get; set; } 
    public string WebSite { get; set; }     
    public string Phone { get; set; }       
    public string Fax { get; set; }       
    public string Address { get; set; }   
}

I do not want it

IEnumerable<spGetCompanies_Result> spGetCompanies();     

I want this.. it's possible ?

IEnumerable<CompanyModel> spGetCompanies(); 

Maybe I can add the class name here?

http://i.hizliresim.com/vQJkkr.png

This example could be the solution?

[Table(Name = "CompanyModel")]
public class CompanyModel
{
    [Column()]
    public int CompanyID { get; set; }        
    [Column()]
    public string SubDomain { get; set; }    
    [Column()] 
    public string CompanyName { get; set; } 
    [Column()]
    public string WebSite { get; set; }     
    [Column()]
    public string Phone { get; set; }       
    [Column()]
    public string Fax { get; set; }       
    [Column()]
    public string Address { get; set; }   
}

I assume you generated the method spGetCompanies when you imported it from the database. That's when the type spGetCompanies_Result was generated. You can change the return type for this method and the mapping will automatically adjusted.

  1. Open your model in the designer.
  2. Right click and open the Model Browser
  3. Double Click the Stored Procedure under Function Imports to open the Function Import Editor (you obviously already found this dialog)
  4. Change the type under 'Returns a Collection of' to your Entity CompanyModel

If you don't have a type CompanyModel as an entity, you could create a new ComplexType with the required properties. Then you wouldn't need the type in your own code.

  1. Right Click the designer and select Add New ComplexType and define all properties.
  2. Select the type in the above dialog

Now you should be able to call a method spGetCompanies that returns IEnumerable<CompanyModel> . At the same time you could change the name to GetCompanies as the model should not reflect that you call a stored procedure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM