Devperience 12.1 ,XPObject创建表之间关系

来源:互联网 发布:滴水编程达人 编辑:程序博客网 时间:2024/05/16 12:31
新建个类文件Student.cs code 如下:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using DevExpress.Xpo;namespace DXWindowsApplication1{     /// <summary>     /// Class 的摘要说明。     /// </summary>    public class Class : XPObject    {        [Association("StudentClasses")]        public Student StudentClassFPK;//外键字段        public string Name;        public int Credit;        public Class()        {            //            // TODO: 在此处添加构造函数逻辑            //        }    }    public class favorite : XPObject    {        [Association("StudentFavorite")]        public Student studentFavoriteFPK;//外键字段        public string fName;    }    public class Student : XPObject    {        string sName;        public string Name         {            get { return sName; }            set { SetPropertyValue<string>("Name", ref sName, value); }        }        int nAge;        public int Age        {            get { return nAge; }            set { SetPropertyValue<int>("Age", ref nAge, value); }        }        /// <summary>        /// Association 第一种及XPCollection,在Association里指明类型        /// </summary>        [Association("StudentClasses", typeof(Class))]        public XPCollection Classes         {             get  {   return GetCollection("Classes"); }         }        /// <summary>        /// Association 第二种及XPCollection,使用泛型T来替换第一种的类型        /// </summary>        [Association("StudentFavorite")]        public XPCollection<favorite> favorites         {            get { return GetCollection<favorite>("favorites"); }        }        public Student()        {            //            // TODO: 在此处添加构造函        }    }}

生成项目,

在Form1.cs

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using DevExpress.Skins;using DevExpress.LookAndFeel;using DevExpress.UserSkins;using DevExpress.XtraBars;using DevExpress.XtraBars.Ribbon;using DevExpress.XtraBars.Helpers;namespace DXWindowsApplication1{    public partial class Form1 : RibbonForm    {        public Form1()        {            InitializeComponent();            for (int i = 1; i < 6; i++)            {                Student sd = new Student();                sd.Name = "Student" + i.ToString();                sd.Age = 2 * i;                Class cls1 = new Class();                Class cls2 = new Class();                Class cls3 = new Class();                favorite favs1 = new favorite();                favorite favs2 = new favorite();                favorite favs3 = new favorite();                cls1.Name = sd.Name + "英语";                cls2.Name = sd.Name + "数学";                cls3.Name = sd.Name + "语文";                cls1.Credit = 5 * sd.Age;                cls2.Credit = 6 * sd.Age;                cls3.Credit = 7 * sd.Age;                sd.Classes.Add(cls1);                sd.Classes.Add(cls2);                sd.Classes.Add(cls3);                favs1.fName = "篮球";                favs2.fName = "足球";                favs3.fName = "排球";                sd.favorites.Add(favs1);                sd.favorites.Add(favs2);                sd.favorites.Add(favs3);                               sd.Save();            }        }    }}

在Program.cs里

using System;using System.Collections.Generic;using System.Windows.Forms;using DevExpress.LookAndFeel;using DevExpress.Xpo;using DevExpress.Xpo.DB;namespace DXWindowsApplication1{    static class Program    {        /// <summary>        /// The main entry point for the application.        /// </summary>        [STAThread]        static void Main()        {            Application.EnableVisualStyles();            Application.SetCompatibleTextRenderingDefault(false);            string forcedConnectionString = @"XpoProvider=MSSqlServer;Data Source=SYFOLINK-PC\MSSQLSERVER2008;User ID=viper;Password=123456;Initial Catalog=test1;Persist Security Info=true";            XpoDefault.DataLayer = XpoDefault.GetDataLayer(forcedConnectionString, AutoCreateOption.DatabaseAndSchema);            Application.Run(new Form1());        }    }}

CTRL+F5,会在DB里生成三个表:Student,class,favorite

可以看到主表是Student,表Class外键StudentClassFPK=表Student的主键OID,同理,表favorite的外键student

将这三个表拉入新建的视图

可以清楚看到关系确定了。

在上面确定了关系并且有了数据了,那么如何获取record呢?

namespace DXWindowsApplication1{    public partial class Form1 : RibbonForm    {        public Form1()        {            InitializeComponent();            this.Text = "";            XPCollection<Class> newxp = new XPCollection<Class>(new BinaryOperator("StudentClassFPK",1, BinaryOperatorType.Equal));            foreach (Class item in newxp)            {                if (item.Name == "Student1语文")                {                     this.Text += item.Credit + "\n" + item.Name.ToString();                }                  }        }    }}


 

上面这段代码获取的是Class表StudentClassFPK=1并且Name=1的数据。

xpCollection<Class>这一句都可以写成XPCollection newxp = new XPCollection(typeof(Class), new BinaryOperator("StudentClassFPK", 1, BinaryOperatorType.Equal));

多条件查询,上面那段代码可以写成

namespace DXWindowsApplication1{    public partial class Form1 : RibbonForm    {        public Form1()        {            InitializeComponent();            this.Text = "";            GroupOperator groupopertor = new GroupOperator();            groupopertor.Operands.Add(new BinaryOperator("StudentClassFPK", 1, BinaryOperatorType.Equal));            groupopertor.Operands.Add(new BinaryOperator("Name", "Student1语文", BinaryOperatorType.Equal));            XPCollection newxp = new XPCollection(typeof(Class),groupopertor);            foreach (Class item in newxp)            {                             this.Text += item.Credit + "\n" + item.Name.ToString();            }        }    }}

this.Text的标题也是表Class的StudentClassFPK=1并且Name=1的数据。

对象的继承:类代码

    /// <summary>    /// 部门职员Staff类    /// </summary>    public class Staff:XPObject    {        string sName;        public string Name         {            get { return sName; }            set { SetPropertyValue<string>("Name", ref sName, value); }        }        int nAge;        public int Age        {            get { return nAge; }            set { SetPropertyValue<int>("Age", ref nAge, value); }        }        [Association("SuervisorStaff")]        public Suervisor suervisor = null;        public Staff()        { }    }    /// <summary>    /// 主管Suervisor类继续Staff员工类    /// </summary>    public class Suervisor:Staff    {        string sDepartment;        public string Department        {            get { return sDepartment; }            set { SetPropertyValue<string>("Department", ref sDepartment, value); }        }        [Association("SuervisorStaff")]        public XPCollection<Staff> staffs        {            get { return GetCollection<Staff>("staffs"); }        }        public Suervisor()        { }    }

插入数据:

 for (int i = 1; i < 11; i++)            {                Suervisor suer = new Suervisor();                suer.Name = "Suervisor" + i.ToString();                suer.Age = 100 * i;                suer.Department = "Suervisor's Department" + i.ToString();                suer.Save();            }            for (int j = 1; j < 20; j++)            {                Staff s = new Staff();                s.Name = "Staff" + j.ToString();                s.Age = j;                s.Save();            }

上面两个循环,第一个是主管类插入10条记录,第二个是员工类类插入19条记录,如果没有继承关系,那么主管表就是10条记录,而员工表19条记录

但是由于主管类继续自员工类,主管都是员工,所以插入员工表时是29条记录了

创建继承的类里,父类多了个ObjectType字段。

插入数据:

后面还有17条


创建表时继承对象的区别http://documentation.devexpress.com/#XPO/CustomDocument3311

There are four classes from which you can derive your persistent objects. These are:XPBaseObject,XPObject,XPLiteObject and XPCustomObject.

Deriving from any class automatically enables various XPO features for your persistent object. For example, when creating a persistent object by deriving from theXPBaseObject,XPCustomObject or XPObject class, an OptimisticLockingAttribute is automatically applied to it. This enables the optimistic locking feature. If your persistent class derives from theXPLiteObject class, the optimistic locking feature is disabled.

The table below shows the difference between the XPO classes.

简单说明一下:继承自XPObject的类创建的表会有自动创建的OID主键(“+”号),而使用XPCustomObject就不会自动创建OID("-"号).

还要去实践过才知道,自己试一试?不过要记得加主键啊,这可没有自动添加的主键了:

 public class baseXPCustomObject : XPCustomObject    {        [Key]        public int k;        public string a;        public string b;        public baseXPCustomObject()        { }    }


如上所示代码,我们要手动加一个主键,用[Key]来表示创建的是主键。并且要输入数据才会在DB里生成表。

 baseXPCustomObject bxpo = new baseXPCustomObject();            bxpo.a = "Hi"; bxpo.b = "viper";            bxpo.Save();


OK,大家都能看到,这不再是生成OID,而是我自己创建的一个主键k.

项目要开始了,今天介绍如何使用来更新代码,版本控制作。小小用了checkout/check in 来更新项目。

字段Code:用来装Description字段值的简写。

 

如何获取放在ArrayList里的GridViewRow,有DevExpress.XtraGrid.Views.Base ,其GridView用方法GetRow(int rowHandle),

根据行索引将获取的行数据存入ar ,ArrayList ar=new ArrayList()

使用 object gridRow_ = ar[i];这个gridRow_调试时发现这个gridRow_有两个字段,

一个是object[]类型 Content,另一个是object类型 OriginalRow

Content里对应的是DB里的数据,OriginalRow是一个实例化对象

我们调试进可以看到gridRow_是值为ReadonlyThreadSafeProxyForObjectFromAnotherThread,所以为了获取值:

 

for (int i = 0; i < ar.Count; i++)        {          object gridRow_ = ar[i];          ReadonlyThreadSafeProxyForObjectFromAnotherThread obj = gridRow_ as ReadonlyThreadSafeProxyForObjectFromAnotherThread;          baseCustomObject  ar_basexpcustomobject=(baseXPCustomObject)obj.OriginalRow;          int j =Convert.ToInt32(ar_basexpcustomobject.pk);
//other codes using j.
}

这样就可以拿到ar[i]这个存放GridViewRow 里的数据(也是类baseXPCustomObject一个实例的Field值)

当遇到Session问题时,看看program.cs里的XpoDefault.Session 的值,如果XpoDefault.Session = null;

那么我们在实例一个类时,要传入Session的一个实例值,Session ss=new Session();

baseXPCustomObject obj=new baseCustomObject(ss);这样才不会有:末实例化的错误。

 

 

 

原创粉丝点击