Using Entity Model for connecting the database

When using Entity Model to connect the database, database is connected to the orject as a data model and instances of databse tables are used to perform insert,update,delete and search operations.

This is a simple example of a database file connected using Entity Model

namespace cecbClasses.Data_Adapters
{
    public class Func_FillMatrix
    {
        DSSEntities cecbContext; // This is the databse context
        LeopoldMatrixValues matrixVal; // This is an variable of a database table

        public Func_FillMatrix()
            {
                cecbContext = new DSSEntities();
            }

// Inserting values to a database table
public bool InsertValues(String impR, String actR, String proR, String impV, String magV)
            {
                bool IsInserted = false;
                try
                {
                    matrixVal = new LeopoldMatrixValues(); // Assigning the variable with a databse object

                    matrixVal.impt_reference = impR; // Assigning values to the attributes of the database table
                    matrixVal.actv_reference = actR;
                    matrixVal.proj_reference = proR;
                    matrixVal.mtrx_importance = double.Parse(impV);
                    matrixVal.mtrx_magnitude = double.Parse(magV);

                    cecbContext.AddToLeopoldMatrixValues(matrixVal);
                    cecbContext.SaveChanges();

                    IsInserted = true;
                }
                catch (Exception)
                {
                    IsInserted = false;  
                }

                return IsInserted;
            }

// Updating values of a database table
public bool UpdateValuesSimple(String impR, String actR, String proR, String impV )
            {
                bool IsInserted = false;
                try
                {

                    SimpleMatrixValues c = cecbContext.SimpleMatrixValues.First(i => i.actv_reference == actR && i.impt_reference == impR && i.proj_reference == proR);
                    c.mtrx_value = int.Parse(impV);
                 
                    cecbContext.SaveChanges();

                    IQueryable<SimpleMatrixValuesExtra> simpleVal =
                                                                from i in cecbContext.SimpleMatrixValuesExtra
                                                                where i.actv_reference == actR &&
                                                                                             i.impt_reference == impR &&
                                                                                             i.proj_reference == proR
                                                                select i; // This is how the query is written

                    foreach (SimpleMatrixValuesExtra x in simpleVal)
                    {
                        cecbContext.DeleteObject(x);
                    }
                    cecbContext.SaveChanges();

                    IsInserted = true;
                }
                catch (Exception)
                {

                    IsInserted = false;
                }

                return IsInserted;
            }
    }
}

Comments

Popular posts from this blog

Dimensional modeling - Star schema vs Fact constellation

Populating data to the Date Dimension table