Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3667454
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:00:10+00:00 2026-05-19T02:00:10+00:00

I have an established SQL Server database setup. I then generated an Entity Framework

  • 0

I have an established SQL Server database setup. I then generated an Entity Framework model in a Console Application to test some selecting, editing and adding to the database. All went well.

I am now moving more towards my final physical design of my WinForms and WebApp. So, I have decided to do the project in separate projects. I moved the Entity Framework to a Data project, I created a Services project, and I still have my console app as a test application (All in the same solution).

I have a ClassLib with data transfer objects to pass between my layers. So my GUI layer and Service layer don’t know of the Entity Framework. I have helper methods in my EF project that convert the EF data into List etc…

Eg of a helper method:

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;

    }
}
}

And my data transfer object looks like this:

namespace ClassLib
{
    public class PayeeDto
    {
        public int PayeeId { get; set; }
        public string Name { get; set; }
        public bool Deleted { get; set; }
    }
}

So, my selection is working well with this design… but… have no idea how to handle saving.

In my console application, when the EF was available to me, I did this:

                        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();

But now I don’t have access to .AddTo…. and .SaveChanges… In my Console app, I’d create a parent object, and then add a few child objects… and then add the child objects to the parent object, and save.

But how would this be done in my new structure? I’m thinking I’d have a Save method in each of my helper classes… And then pass a List<> of the child objects, along with a single Parent class to the save method… and then transfor the Dtos to EF models, and then save it that way?

Is that an acceptable plan?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T02:00:11+00:00Added an answer on May 19, 2026 at 2:00 am

    I only use DTO objects to transfer data from A to B. The updating, adding, removing etc., I always encapsulate in Commands (Command Pattern).
    Retrieving data I do similarily with “Helper” classes.

    Example of command pattern:

    The base classes:

    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;
                }
    
            }
    }
    

    Implementation Example:

    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;
    
            }
        }
    }
    

    Usage:

    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();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database working in my local sql server 2005 express edition. I
I have an application which has 3 services which are dependent on SQL server.
Have you ever seen any of there error messages? -- SQL Server 2000 Could
I am trying to import CSV file to SQL server database, with no success.
I have two SQL Server 2005 databases, one is for development and the other
I have established a link using Netbeans and Paradox driver and can query the
Have you used VS.NET Architect Edition's Application and System diagrams to start designing a
Have a n-tire web application and search often times out after 30 secs. How
Is it possible to configure the Windows Workflow SqlWorkflowPersistanceService to use different SQL Server
I'm looking for a tool (best for free) which can synchronize two SQL Server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.