Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.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# 不同项目上的实体框架-保存?_C#_Entity Framework - Fatal编程技术网

C# 不同项目上的实体框架-保存?

C# 不同项目上的实体框架-保存?,c#,entity-framework,C#,Entity Framework,我已经建立了SQL Server数据库设置。然后,我在控制台应用程序中生成了一个实体框架模型,以测试一些选择、编辑和添加到数据库中的操作。一切顺利 我现在更倾向于我的WinForms和WebApp的最终物理设计。因此,我决定在单独的项目中进行该项目。我将实体框架移动到一个数据项目,创建了一个服务项目,并且我仍然将我的控制台应用程序作为测试应用程序(都在同一个解决方案中) 我有一个ClassLib,其中包含要在层之间传递的数据传输对象。所以我的GUI层和服务层不知道实体框架。我在我的EF项目中有助

我已经建立了SQL Server数据库设置。然后,我在控制台应用程序中生成了一个实体框架模型,以测试一些选择、编辑和添加到数据库中的操作。一切顺利

我现在更倾向于我的WinForms和WebApp的最终物理设计。因此,我决定在单独的项目中进行该项目。我将实体框架移动到一个数据项目,创建了一个服务项目,并且我仍然将我的控制台应用程序作为测试应用程序(都在同一个解决方案中)

我有一个ClassLib,其中包含要在层之间传递的数据传输对象。所以我的GUI层和服务层不知道实体框架。我在我的EF项目中有助手方法,可以将EF数据转换为列表等

辅助方法的示例:

using ClassLib;

namespace Data
{
    public class PayeeDAL : EntityBase
    {

    public static List<PayeeDto> GetPayees()
    {
        var payees = (from payee in Db.payees
                      select payee).ToList();

        List<PayeeDto> reply = new List<PayeeDto>();

        foreach (var pa in payees)
        {
            PayeeDto p = new PayeeDto
                             {
                                 PayeeId = pa.payee_id,
                                 Name = pa.name,
                                 Deleted = pa.deleted == null
                             };
            reply.Add(p);
        }
        return reply;

    }
}
}
所以,我的选择与这个设计配合得很好。。。但是不知道如何处理储蓄

在我的控制台应用程序中,当EF对我可用时,我执行了以下操作:

                        db.AddToaccount_transaction(ac);

                    account_transaction_line atl = new account_transaction_line
                    {
                        amount = amount,
                        cost_centre =
                            db.cost_centre.FirstOrDefault(
                                cc => cc.cost_centre_id == costcentreid),
                        sub_category =
                            db.sub_category.First(
                                sc => sc.sub_category_id == subcategoryId),
                        account_transaction = ac,
                        budget = db.budgets.FirstOrDefault(b => b.budget_id == budgetid)

                    };

                    db.AddToaccount_transaction_line(atl);


                }



                db.SaveChanges();
但现在我无法访问.AddTo。。。。和。保存更改。。。在我的控制台应用程序中,我会创建一个父对象,然后添加几个子对象。。。然后将子对象添加到父对象,然后保存

但在我的新结构中,这将如何实现?我想在我的每个助手类中都有一个Save方法。。。然后将子对象列表以及单个父类传递给save方法。。。然后将DTO转换为EF模型,然后以这种方式保存


这是一个可接受的计划吗?

我只使用DTO对象将数据从A传输到B。更新、添加、删除等,我总是封装在命令中(命令模式)。 检索数据的方法与“Helper”类类似

命令模式示例:

基类:

namespace Busker.Data.Commands
{
    /// <summary>
    /// The 'Command' abstract class
    /// </summary>
    public abstract class Command
    {

        private string message = "";
        public string Message
        {
            get { return message; }
            set { message = value; }
        }


        private bool success = false;
        public bool Success
        {
            get { return success; }
            set { success = value; }
        }

        private Validator validator = new Validator();
        public Validator Validator
        {
            get { return validator; }
            set { validator = value; }
        }

        private CommandStatusCode statusCode = CommandStatusCode.OK;
        public CommandStatusCode StatusCode
        {
            get { return statusCode; }
            set { statusCode = value; }
        }

        public LoggingLevel LoggingLevel = LoggingLevel.Debug;


        //public BuskerContext BuskerContext;


        public bool IsValid()
        {
            if (validator.Errors.Count > 0)
                return false;
            return true;

        }

        public abstract void Execute();

        public void FailedSubCommand(Command cmd) 
        {
            this.Success = cmd.Success;
            this.Message = cmd.message;
        }
    }
}


namespace Busker.Data.Commands
{
    public class Invoker
    {
        private Command command;

        public Command Command
        {
            get { return command; }
            set { command = value; }
        }


        public void SetCommand(Command command)
        {
            this.Command = command;
        }

        public virtual void ExecuteCommand()
        {
            if (command == null)
                throw new Exception("You forgot to set the command!!");

            try
            {
                log(this.command.GetType().Name + " starting execution ");
                command.Execute();
                if (!command.Success)
                {
                    log(this.command.GetType().Name + " completed execution but failed. Message: " + command.Message + " " + command.StatusCode.ToString());
                }
                else
                    log(this.command.GetType().Name + " completed execution. Success!");

            }
            catch (Exception ex)
            {
                command.StatusCode = CommandStatusCode.Error;
                Loggy.AddError("An unhandled error was caught in " + this.command.GetType().Name + ": " + ex.Message, ex);
                command.Message = ex.ToString();
                //throw;
            }
        }

        private void log(string msg)
        {
            switch (command.LoggingLevel)
            {
                case Busker.Data.Constants.LoggingLevel.Debug:
                    Loggy.Debug(msg);
                    break;
                case Busker.Data.Constants.LoggingLevel.Off:
                    break;
                default:
                    Loggy.Add(msg);
                    break;

            }

        }

        public virtual void ExecuteLinqCommand()
        {
            this.ExecuteCommand();
        }
    }
}

namespace Busker.Data.Commands
{
  public static class Extensions
  {
        /// <summary>
        /// Executes the command using the default invoker.
        /// </summary>
        /// <param name="aCommand"></param>
        public static void Invoke(this Command aCommand)
        {
            System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace();
            System.Reflection.MethodBase m = stackTrace.GetFrame(1).GetMethod();
            String strMethodName = m.DeclaringType.Name + "." + m.Name;

            try
            {
                Invoker invoker = new Invoker();
                invoker.SetCommand(aCommand);
                invoker.ExecuteCommand();
            }
            catch (Exception ex)
            {
                Loggy.AddError("An error occured in Extensions.Invoke. + " + strMethodName ,ex);
                throw ex;
            }

        }
}
用法:

CreateMessageCommand  cmd = new CreateMessageCommand (...);
//Don't use the execute method of the command
//the invoker, because implemented as an extension can be exchange in different 
//environments
cmd.Invoke();

我不太明白-所以我搜索了更多,发现了一个教程,显示了一个人使用控制台应用程序做EF应用程序。她直接从控制台应用程序引用了EF项目。这很容易,但是。。。这似乎是错误的。当然应该有一个服务层,GUI项目和EF项目之间没有交互,对吗?最终取决于你,你觉得什么是对的。我在项目中做过这两种变体,我的简历是:如果你想快速产生结果,或者你有一个高度分散的开发团队,他们完全理解这个概念,那么直接访问它。否则,请使用服务层。。(我的代码中是否有您不理解的内容?)
namespace Busker.Data.Commands.Message
{
    public class CreateMessageCommand :Command
    {


        public CreateMessageCommand (int from, int to, string title, string body)
        {
                // set private variable.. 
        }


        public override void Execute()
        {

            // Do your stuff here


            be.SaveChanges();
            this.Success = true;

        }
    }
}
CreateMessageCommand  cmd = new CreateMessageCommand (...);
//Don't use the execute method of the command
//the invoker, because implemented as an extension can be exchange in different 
//environments
cmd.Invoke();