Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC4中的自动映射无限循环_C#_Asp.net Mvc_Asp.net Mvc 4_Automapper_Stack Overflow - Fatal编程技术网

C# MVC4中的自动映射无限循环

C# MVC4中的自动映射无限循环,c#,asp.net-mvc,asp.net-mvc-4,automapper,stack-overflow,C#,Asp.net Mvc,Asp.net Mvc 4,Automapper,Stack Overflow,我在mvc项目中遇到堆栈溢出异常 我在Global.asax文件中调用MapInitialize.Initialize 这是MapInitialize类 using AutoMapper; using Model1 = Project.BusinessLogic.Model; using Model2 = Project.DataAccess.Model; namespace Project.BusinessLogic { public static class MapInitiali

我在mvc项目中遇到堆栈溢出异常

我在Global.asax文件中调用MapInitialize.Initialize

这是MapInitialize类

using AutoMapper;
using Model1 = Project.BusinessLogic.Model;
using Model2 = Project.DataAccess.Model;

namespace Project.BusinessLogic
{
    public static class MapInitialize
    {
        public static void Initialize()
        {
            Mapper.CreateMap<Model1.table, Model2.table>();
        }
    }
}
使用AutoMapper;
使用Model1=Project.BusinessLogic.Model;
使用Model2=Project.DataAccess.Model;
名称空间项目.BusinessLogic
{
公共静态类MapInitialize
{
公共静态void Initialize()
{
CreateMap();
}
}
}
这是Model1.tables字段的分部类

    namespace Project.BusinessLogic
    {
        public partial class table
        {
            public int ID { get; set; }
            public string field1{ get; set; }
            public string field2 { get; set; }
            public string field3{ get; set; }

            public virtual table2 table2 { get; set; }
        }
    }


    using Model = Project.DataAccess.Model;
    namespace Project.BusinessLogic
    {        
        public partial class table
        {
             public static implicit operator Model.table(table model)
             {
                 if (model == null) return null;
                 return Mapper.Map<table, Model.table>(model);
             }

             public static implicit operator table(Model.table model)
             {
                 if (model == null) return null;
                 return Mapper.Map<Model.table, table>(model);
             }
         }
     }
namespace Project.BusinessLogic
{
公共部分类表
{
公共int ID{get;set;}
公共字符串字段1{get;set;}
公共字符串字段2{get;set;}
公共字符串字段3{get;set;}
公共虚拟表2表2{get;set;}
}
}
使用Model=Project.DataAccess.Model;
名称空间项目.BusinessLogic
{        
公共部分类表
{
公共静态隐式运算符模型.表(表模型)
{
if(model==null)返回null;
返回Mapper.Map(模型);
}
公共静态隐式运算符表(Model.table Model)
{
if(model==null)返回null;
返回Mapper.Map(模型);
}
}
}
我得到了一个例外

 public Result<Model.table> Createtable(Model.table model)
{
    var result = new Result<Model.table>();
    try
    {
        model.field1 = "foo";
        model.field2 = "foo2";
        var res = Ctx.table.Add(model);
        Ctx.SaveChanges();
        result.Value = res; ***// here is infinity loop exception*** 
        result.Success = result.Value != null;
        return result;
    }
    catch (Exception ex)
    {
       Console.Log(ex);
    }
}
public Result Createtable(Model.table Model)
{
var result=新结果();
尝试
{
model.field1=“foo”;
model.field2=“foo2”;
var res=Ctx.table.Add(模型);
Ctx.SaveChanges();
result.Value=res;***//这里是无限循环异常***
result.Success=result.Value!=null;
返回结果;
}
捕获(例外情况除外)
{
控制台日志(ex);
}
}

使用强制转换操作符并不总是一个好主意,因为这样很难看到正在执行的代码。当您确实使用它们时,最好不要使用双向
隐式
强制转换运算符。尝试使其中一个
显式
。这将减少无限循环的可能性

话虽如此,我认为在您的情况下,如果您显式地添加
Mapper.Map
调用,而不是依赖cast操作符,那么其他人阅读代码会更加清楚