DevExpress Xaf入門——關於一對多關系的配置(DC模式)


1、在VS2012中通過新建XAF解決方案Solution2,並在Solution2.Web項目中的配置文件Web.config中配置連接字符串;

  <connectionStrings>
    <add name="EasyTestConnectionString" connectionString="Integrated Security=SSPI;Pooling=false;Data Source=.\SQLEXPRESS;Initial Catalog=Solution2EasyTest" />
    <add name="ConnectionString" connectionString="Data Source=GUANBAOPC\SQLEXPRESS;Initial Catalog=Solution2DB;Persist Security Info=true;User ID=sa;Password=123" />
  </connectionStrings>

 

2、在解決方案Solution2中對應的項目Solution2.Module的文件夾BusinessObjects中新建Xaf Domain Component對象Department、Employee。

Department對象(接口)

using System;
using System.Linq;
using System.Text;
using System.ComponentModel;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.DC;
using DevExpress.Data.Filtering;
using DevExpress.Persistent.Base;
using System.Collections.Generic;
using DevExpress.ExpressApp.Model;
using DevExpress.Persistent.Validation;

namespace Solution2.Module.BusinessObjects
{
    [DomainComponent]
    [DefaultClassOptions]
    [XafDisplayName("部門信息")]
    public interface Department
    {
        [XafDisplayName("部門名稱")]
        string DepartmentName { get; set; }

        [XafDisplayName("部門編號")]
        string DepartmentCode { get; set; }

        [XafDisplayName("聯系電話")]
        string ContactNumber { get; set; }

        [XafDisplayName("部門員工")]
        IList<Employee> DepartmentEmployees { get; }
    }
}

Employee對象(接口)

using System;
using System.Linq;
using System.Text;
using System.ComponentModel;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.DC;
using DevExpress.Data.Filtering;
using DevExpress.Persistent.Base;
using System.Collections.Generic;
using DevExpress.ExpressApp.Model;
using DevExpress.Persistent.Validation;

namespace Solution2.Module.BusinessObjects
{
    [DomainComponent]
    [DefaultClassOptions]
    [XafDisplayName("員工信息")]
    public interface Employee
    {
        [XafDisplayName("員工姓名")]
        string EmployeeName { get; set; }

        [XafDisplayName("工號")]
        string JobNumber { get; set; }

        [XafDisplayName("部門")]
        Department Department { get; set; }
    }
}

3、右鍵Solution2.Module中的Module.cs文件,點擊查看代碼,在打開的Module.cs文檔中注冊Employee與Department對象。

using System;
using System.Text;
using System.Linq;
using DevExpress.ExpressApp;
using System.ComponentModel;
using DevExpress.ExpressApp.DC;
using System.Collections.Generic;
using DevExpress.Persistent.Base;
using DevExpress.ExpressApp.Model;
using DevExpress.ExpressApp.Actions;
using DevExpress.ExpressApp.Editors;
using DevExpress.ExpressApp.Updating;
using DevExpress.ExpressApp.Model.Core;
using DevExpress.ExpressApp.Model.DomainLogics;
using DevExpress.ExpressApp.Model.NodeGenerators;
using Solution2.Module.BusinessObjects;

namespace Solution2.Module
{
    public sealed partial class Solution2Module : ModuleBase
    {
        public Solution2Module()
        {
            InitializeComponent();
        }
        public override IEnumerable<ModuleUpdater> GetModuleUpdaters(IObjectSpace objectSpace, Version versionFromDB)
        {
            ModuleUpdater updater = new DatabaseUpdate.Updater(objectSpace, versionFromDB);
            return new ModuleUpdater[] { updater };
        }
        public override void Setup(XafApplication application)
        {
            base.Setup(application);
            XafTypesInfo.Instance.RegisterEntity("Employee", typeof(Employee)); XafTypesInfo.Instance.RegisterEntity("Department", typeof(Department));
        }
    }
}

 

4、將Solution2.Web設為啟動項目,並使用調試模式運行解決方案。

提示:

1、部門對象(Deparment)中部門員工屬性(DepartmentEmployees)不能設置set方法;

2、員工對象(Employee)中部門員工屬性(DepartmentEmployees)類型IList<Employee>不能配置為List<Employee>;

3、員工對象(Employee)中必須存在部門屬性(Department)且必須帶有get和set方法。

4、第一次運行解決方案會彈出錯誤提示,重新運行解決方案后員工與部門的一對多關系的界面就出來了。

5、必須將Solution2.Web設為啟動項目。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM