0

我试图更改实体模型或数据库中的表名,但旧名称已在应用程序的许多地方使用。有没有办法在 LINQ 查询或代码中自动反映重命名的实体或表。

假设我有表 tblDepartment、tblEmployee 和 tblEmployeeDepartment。这些表在许多地方的代码(LINQ)中使用。我喜欢将这些表名称更改为 Department、Employee 和 EmployeeDepartment。那么,当我使用数据库优先或模型优先方法更改表名时,无论如何都会在 LINQ 或代码中自动反映名称。

PS该应用程序基于.Net 3.5

4

3 回答 3

0

使用 xml | 添加、修改和删除xml数据

    string conn = "E:/School/Development Sftware/2014/ReadWriteUpdateDeleteXML/DataService/Profiles.xml";
    public string InsertProfile(string fname, string lname, string phone, string gender)
    {

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(conn);
        XmlElement subRoot = xmlDoc.CreateElement("Profile");

        //add first name
        XmlElement appendedElementFname = xmlDoc.CreateElement("FirstName");
        XmlText xmlTextFname = xmlDoc.CreateTextNode(fname.Trim());
        appendedElementFname.AppendChild(xmlTextFname);
        subRoot.AppendChild(appendedElementFname);
        xmlDoc.DocumentElement.AppendChild(subRoot);

        //add last name
        XmlElement appendedElementLname = xmlDoc.CreateElement("LastName");
        XmlText xmlTextLname = xmlDoc.CreateTextNode(lname.Trim());
        appendedElementLname.AppendChild(xmlTextLname);
        subRoot.AppendChild(appendedElementLname);
        xmlDoc.DocumentElement.AppendChild(subRoot);

        //add phone
        XmlElement appendedElementPhone = xmlDoc.CreateElement("Phone");
        XmlText xmlTextPhone = xmlDoc.CreateTextNode(phone.Trim());
        appendedElementPhone.AppendChild(xmlTextPhone);
        subRoot.AppendChild(appendedElementPhone);
        xmlDoc.DocumentElement.AppendChild(subRoot);

        //add gender
        XmlElement appendedElementGender = xmlDoc.CreateElement("Gender");
        XmlText xmlTextGender = xmlDoc.CreateTextNode(gender.Trim());
        appendedElementGender.AppendChild(xmlTextGender);
        subRoot.AppendChild(appendedElementGender);
        xmlDoc.DocumentElement.AppendChild(subRoot);

        xmlDoc.Save(conn);

        return "Profile Saved";
    }
    public DataSet LoadXML()
    {
        DataSet dsLog = new DataSet();
        dsLog.ReadXml(conn);
        return dsLog;
    }
    public string DeleteProfile(string fname)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(conn);

        //XmlNode nodeToDelete = xmlDoc.SelectSingleNode("/Profiles/Profile[@FirstName=" + fname + "]");
        //if (nodeToDelete != null)
        //{
        //    nodeToDelete.ParentNode.RemoveChild(nodeToDelete);
        //}
        //xmlDoc.Save("C:/Users/Shazzy/Documents/Visual Studio 2010/Projects/ReadWriteUpdateDeleteXML/DataService/Profiles.xml");
        //return "Deleted";

        foreach (XmlNode node in xmlDoc.SelectNodes("Profiles/Profile"))
        {
            if (node.SelectSingleNode("FirstName").InnerText == fname)
            {
                node.ParentNode.RemoveChild(node);
            }

        }
        xmlDoc.Save(conn);

        return "Deleted";
    }
    public string ModifyProfile(string fname, string lname, string phone, string gender)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(conn);

        foreach (XmlNode node in xmlDoc.SelectNodes("Profiles/Profile"))
        {
            if (node.SelectSingleNode("FirstName").InnerText == fname)
            {
                node.SelectSingleNode("FirstName").InnerText = fname;
                node.SelectSingleNode("LastName").InnerText = lname;
                node.SelectSingleNode("Phone").InnerText = phone;
                node.SelectSingleNode("Gender").InnerText = gender;
            }
        }
        xmlDoc.Save(conn);

        return "Updated";
    }
于 2014-06-11T19:55:50.210 回答
0

使用 linq 和 Entity Framework 以及 excel 报告|享受

    public static string strMessage = "";
    public SchoolEntities dbContext;

    public string login(string strUsername, string strPassword)
    {
        dbContext = new SchoolEntities();

        var linqQuery = from User in dbContext.People
                        where User.FirstName == strUsername && User.LastName == strPassword
                        select User;

        if (linqQuery.Count() == 1)
        {
            strMessage = "Good";
        }
        else
        {
            strMessage = "Bad";
        }

        return strMessage;

    }
    public Object LoadPersonDetails()
    {
        dbContext = new SchoolEntities();

        //DataTable dtPerson = new DataTable();

        var linqQuery = from users in dbContext.People
                        select users;

        //List<Person> Users = linqQuery.ToList();

        //dtPerson = linqQuery.ToList();

        return linqQuery;
    }

    public void InsertPerson(string strLName, string strFName, string strHireDate, string EnrollmentDate)
    {
        dbContext = new SchoolEntities();
        Person NewPerson = dbContext.People.Create();

        NewPerson.LastName = strLName;
        NewPerson.FirstName = strFName;
        NewPerson.HireDate = Convert.ToDateTime(strHireDate);
        NewPerson.EnrollmentDate = Convert.ToDateTime(EnrollmentDate);

        dbContext.People.Add(NewPerson);
        dbContext.SaveChanges();
    }
    public void DeleteUser(int intPersonID)
    {
        //dbContext = new SchoolEntities();
        using (dbContext = new SchoolEntities())
        {
            Person Person = dbContext.People.Where(c => c.PersonID == intPersonID).FirstOrDefault();
            if (Person != null)
            {
                dbContext.People.Remove(Person);
                dbContext.SaveChanges();
            }
        }
    }
    public void ModifyPerson(int intPersonID, string strLName, string strFName, string strHireDate, string EnrollmentDate)
    {
        var UpdatePerson = dbContext.People.FirstOrDefault(s => s.PersonID == intPersonID);

        UpdatePerson.LastName = strLName;
        UpdatePerson.FirstName = strFName;
        UpdatePerson.HireDate = Convert.ToDateTime(strHireDate);
        UpdatePerson.EnrollmentDate = Convert.ToDateTime(EnrollmentDate);

        dbContext.SaveChanges();
    }

    private Excel.Application XApp = null; //Creates the Excel Document
    private Excel.Workbook XWorkbook = null; //create the workbook in the recently created document
    private Excel.Worksheet XWorksheet = null; //allows us to work with current worksheet
    private Excel.Range XWorkSheet_range = null; // allows us to modify cells on the sheet

    public void Reports()
    {
        dbContext = new SchoolEntities();

        var linqQuery = (from users in dbContext.StudentGrades
                         group users by new { users.EnrollmentID, users.CourseID, users.StudentID, users.Grade }
                         into UserGroup
                         orderby UserGroup.Key.CourseID ascending
                         select new { UserGroup.Key.EnrollmentID, UserGroup.Key.CourseID, UserGroup.Key.StudentID, UserGroup.Key.Grade }).ToList();

        var RatingAverage = dbContext.StudentGrades.Average(r => r.Grade);

        var GradeSum = dbContext.StudentGrades.Sum(r => r.Grade);

        /*var linqQuery = (from users in dbContext.StudentGrades
                         orderby users.CourseID descending
                         select users).ToList();*/

        //Array Motho = linqQuery.ToArray();

        XApp = new Excel.Application();
        XApp.Visible = true;
        XWorkbook = XApp.Workbooks.Add(1);
        XWorksheet = (Excel.Worksheet)XWorkbook.Sheets[1]; 

        //Create column headers
        XWorksheet.Cells[2, 1] = "EnrollmentID";
        XWorksheet.Cells[2, 2] = "CourseID";
        XWorksheet.Cells[2, 3] = "StudentID";
        XWorksheet.Cells[2, 4] = "Grade";
        //XWorksheet.Cells[2, 5] = "Enrollment Date";

        int row = 3;

        foreach (var Mothos in linqQuery)
        {
            XWorksheet.Cells[row, 1] = Mothos.EnrollmentID.ToString();
            XWorksheet.Cells[row, 2] = Mothos.CourseID.ToString();
            XWorksheet.Cells[row, 3] = Mothos.StudentID.ToString();
            XWorksheet.Cells[row, 4] = Mothos.Grade.ToString();

            row++;
        }

        int rows = linqQuery.Count();

        XWorksheet.Cells[rows + 4, 3] = "Grades Average";
        XWorksheet.Cells[rows + 4, 4] = RatingAverage.Value.ToString();

        XWorksheet.Cells[rows + 5, 3] = "Grades Sum";
        XWorksheet.Cells[rows + 5, 4] = GradeSum.Value.ToString();

        //XWorkSheet_range.ColumnWidth = 30;

        //XWorksheet.Cells.AutoFit();
    }
于 2014-03-20T06:50:01.110 回答
0

使用 edm 和 Excel 分组报告


public void Function_Create_Sales()
        { 
        
        DBContext = new PubsEntities();

        var linqStores = from Sales in DBContext.sales
                         orderby Sales.stor_id
                         select Sales;

        var lstStore = linqStores.ToList();

        Excel.Application xlApp = new Excel.Application();
        xlApp.Visible = true;
        Excel.Workbook xlBook = xlApp.Workbooks.Add(1);
        Excel.Worksheet xlSheet = (Excel.Worksheet)xlBook.Worksheets[1];

        int GroupTotal = 0;
        int GrandTotal = 0;
        int ExcelRow = 5;
        string intTemp = lstStore[0].stor_id;
        xlSheet.Cells[4, 1] = lstStore[0].stor_id;

        //Create column headers
        xlSheet.Cells[1, 1] = "Sales Grouped By Store";
        xlSheet.Cells[3, 1] = "Group header";
        xlSheet.Cells[3, 2] = "Store ID";
        xlSheet.Cells[3, 3] = "Order Number";
        xlSheet.Cells[3, 4] = "Order Date";
        xlSheet.Cells[3, 5] = "Quantity";
        xlSheet.Cells[3, 6] = "payments";
        xlSheet.Cells[3, 7] = "title ID";

        for (int count = 0; count < lstStore.Count; count++)
        {
            if (intTemp == lstStore[count].stor_id)
            {
                xlSheet.Cells[ExcelRow, 2] = lstStore[count].stor_id.ToString();
                xlSheet.Cells[ExcelRow, 3] = lstStore[count].ord_date.ToString();
                xlSheet.Cells[ExcelRow, 4] = lstStore[count].qty.ToString();
                xlSheet.Cells[ExcelRow, 5] = lstStore[count].payterms.ToString();
                xlSheet.Cells[ExcelRow, 5] = lstStore[count].title_id.ToString();

                ExcelRow++;
                GroupTotal++;
                GrandTotal++;
            }
            else
            {
                xlSheet.Cells[ExcelRow, 5] = "Total for: " + intTemp + " = " + GroupTotal.ToString();
                ExcelRow++;
                intTemp = lstStore[count].stor_id;
                xlSheet.Cells[ExcelRow, 1] = lstStore[count].stor_id;
                count--;
                GroupTotal = 0;
                ExcelRow++;
            }
        }
        xlSheet.Cells[ExcelRow, 5] = "Total for: " + intTemp + " = " + GroupTotal.ToString();
        ExcelRow++;
        xlSheet.Cells[ExcelRow, 5] = "Grand Total = " + GrandTotal.ToString();

        xlSheet.Rows.Columns.AutoFit();
    }
于 2014-06-11T19:50:55.013 回答