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中对应的项目Solution1.Module的文件夹BusinessObjects中新建Xaf Domain Component对象Student、Course。
Student对象(接口)
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 Student { [XafDisplayName("学生姓名")] string StudentName { get; set; } [XafDisplayName("学号")] string StudentCode { get; set; } [XafDisplayName("课程")] IList<Course> Courses { get; } } }
Course对象(接口)
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 Course { [XafDisplayName("课程名称")] string CourseName { get; set; } [XafDisplayName("课程编号")] string CourseCode { get; set; } [XafDisplayName("学生")] IList<Student> Students { get; } } }
3、右键Solution2.Module中的Module.cs文件,点击查看代码,在打开的Module.cs文档中注册Student与Course对象。
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("Student", typeof(Student)); XafTypesInfo.Instance.RegisterEntity("Course", typeof(Course)); } } }
4、将Solution2.Web设为启动项目,并使用调试模式运行解决方案。运行结果如下:
提示:
1、学生对象(Student)中课程属性(Course)不能设置set方法;
2、课程对象(Course)中学生属性(Student)不能设置set方法;
3、IList类型不能以List代替;
4、第一次运行解决方案会弹出错误提示,重新运行解决方案后学生与课程的多对多关系的界面就出来了;
5、必须将Solution2.Web设为启动项目。