Asp.net mvc 4 如何在Web API中强制转换实体框架对象?

Asp.net mvc 4 如何在Web API中强制转换实体框架对象?,asp.net-mvc-4,entity-framework-4,asp.net-web-api,Asp.net Mvc 4,Entity Framework 4,Asp.net Web Api,我使用的是实体框架,我发现它无法序列化 EDM对象。现在我使用的是Northwind产品表。因此,我被迫将对象投射到另一个对象,并使用.cast,但它不起作用。 我唯一的解决方案是在代码中逐个属性手动执行,但我在想——一定有更好的方法! 看在上帝的份上,现在是2013年!这个实体在一开始看起来是个好主意,但是它有太多的陷阱和约束,它实际上阻碍了比帮助更多的东西,但是无论如何,EDMX图表是很好的 有谁有更好的方法来铸造这些物体吗 POCO public class Product

我使用的是实体框架,我发现它无法序列化 EDM对象。现在我使用的是Northwind产品表。因此,我被迫将对象投射到另一个对象,并使用.cast,但它不起作用。 我唯一的解决方案是在代码中逐个属性手动执行,但我在想——一定有更好的方法! 看在上帝的份上,现在是2013年!这个实体在一开始看起来是个好主意,但是它有太多的陷阱和约束,它实际上阻碍了比帮助更多的东西,但是无论如何,EDMX图表是很好的

有谁有更好的方法来铸造这些物体吗

POCO

   public class Product
    {
        public int ProductID { get; set; }
        public string ProductName { get; set; }
        //public Nullable<int> SupplierID { get; set; }
        //public Nullable<int> CategoryID { get; set; }
        public string QuantityPerUnit { get; set; }
        public Nullable<decimal> UnitPrice { get; set; }
        public Nullable<short> UnitsInStock { get; set; }
        public Nullable<short> UnitsOnOrder { get; set; }
        public Nullable<short> ReorderLevel { get; set; }
        //public bool Discontinued { get; set; }

        public Category Category { get; set; }
        //public ICollection<Order_Detail> Order_Details { get; set; }
        //public Supplier Supplier { get; set; }




    }
   public class ProductsViewModel
    {

        public List<POCO.Product> Products { get; set; }


        public ProductsViewModel()
        {
           using (NorthwindEntities dNorthwindEntities = new NorthwindEntities())
           {
               this.Products = dNorthwindEntities.Products.Cast<POCO.Product>().ToList(); 
 public class ProductsController : ApiController
    {



            public List<Product> GetAllProducts()
            {
                var viewmodel = new ProductsViewModel();
                return viewmodel.Products; 
            }
公共类产品
{
public int ProductID{get;set;}
公共字符串ProductName{get;set;}
//公共可为空的供应商ID{get;set;}
//公共可空类别ID{get;set;}
公共字符串QuantityPerUnit{get;set;}
公共可为空的单价{get;set;}
公共可空单位库存{get;set;}
公共可为空的UnitsOnOrder{get;set;}
公共可为空的重新排序级别{get;set;}
//公共布尔已中断{get;set;}
公共类别{get;set;}
//公共ICollection Order_Details{get;set;}
//公共供应商{get;set;}
}
查看模型

   public class Product
    {
        public int ProductID { get; set; }
        public string ProductName { get; set; }
        //public Nullable<int> SupplierID { get; set; }
        //public Nullable<int> CategoryID { get; set; }
        public string QuantityPerUnit { get; set; }
        public Nullable<decimal> UnitPrice { get; set; }
        public Nullable<short> UnitsInStock { get; set; }
        public Nullable<short> UnitsOnOrder { get; set; }
        public Nullable<short> ReorderLevel { get; set; }
        //public bool Discontinued { get; set; }

        public Category Category { get; set; }
        //public ICollection<Order_Detail> Order_Details { get; set; }
        //public Supplier Supplier { get; set; }




    }
   public class ProductsViewModel
    {

        public List<POCO.Product> Products { get; set; }


        public ProductsViewModel()
        {
           using (NorthwindEntities dNorthwindEntities = new NorthwindEntities())
           {
               this.Products = dNorthwindEntities.Products.Cast<POCO.Product>().ToList(); 
 public class ProductsController : ApiController
    {



            public List<Product> GetAllProducts()
            {
                var viewmodel = new ProductsViewModel();
                return viewmodel.Products; 
            }
公共类产品视图模型
{
公共列表产品{get;set;}
公共产品视图模型()
{
使用(NorthwindEntities dNorthwindEntities=新的NorthwindEntities())
{
this.Products=dNorthwindEntities.Products.Cast().ToList();
Web api控制器:

   public class Product
    {
        public int ProductID { get; set; }
        public string ProductName { get; set; }
        //public Nullable<int> SupplierID { get; set; }
        //public Nullable<int> CategoryID { get; set; }
        public string QuantityPerUnit { get; set; }
        public Nullable<decimal> UnitPrice { get; set; }
        public Nullable<short> UnitsInStock { get; set; }
        public Nullable<short> UnitsOnOrder { get; set; }
        public Nullable<short> ReorderLevel { get; set; }
        //public bool Discontinued { get; set; }

        public Category Category { get; set; }
        //public ICollection<Order_Detail> Order_Details { get; set; }
        //public Supplier Supplier { get; set; }




    }
   public class ProductsViewModel
    {

        public List<POCO.Product> Products { get; set; }


        public ProductsViewModel()
        {
           using (NorthwindEntities dNorthwindEntities = new NorthwindEntities())
           {
               this.Products = dNorthwindEntities.Products.Cast<POCO.Product>().ToList(); 
 public class ProductsController : ApiController
    {



            public List<Product> GetAllProducts()
            {
                var viewmodel = new ProductsViewModel();
                return viewmodel.Products; 
            }
公共类产品控制器:ApiController
{
公共列表GetAllProducts()
{
var viewmodel=新产品viewmodel();
返回viewmodel.Products;
}
1。 您可以使用像
AutoMapper
这样的框架来自动处理
实体到
ViewModel/DTO
映射

二,。 由于以下几个原因,不建议在
视图中使用
实体
(即使是在其POCO形式中):

  • 安全性
    :将实体发送回客户端/视图可能会暴露比预期更多的数据

  • 序列化
    :由于您的实体通常包含对另一个实体的引用,而这些实体可能包含对(父)实体的引用,因此您必须配置序列化程序来处理这种情况,否则将出现
    循环依赖项异常

  • 不兼容
    :您的实体结构可能与您的
    视图/客户端
    需要呈现的内容不兼容。有时您的
    视图
    只需要一个简单的
    字符串
    ,而实体以一种非常复杂的方式保存此数据,因此
    视图
    需要“提取”它,您最终会得到一个
    视图
    充满了不必要的实体向下钻取代码