合并表/文件

时间:2014-01-17 20:01:16

标签: entity-framework unidata u2netdk

我首先使用实体​​框架代码并将我们的unidata文件映射到表以获取数据。我想加入表格或使用导航属性。这两个表共享一个名为WorkInProgressOperationId的公共字段。我尝试使用连接和导航属性,但在映射到unidata文件时似乎不起作用。这可能吗?

        public class WorkInProgressMapping : EntityTypeConfiguration<WorkInProgress>
{

    public WorkInProgressMapping()
    {          
        this.ToTable("WIPMASTER");
        this.HasKey(e => e.WorkInProgressId).Ignore(e => e.EntityId);           
        this.Property(e => e.WorkInProgressId).HasColumnName("@ID");
        this.Property(e => e.SequenceNumber).HasColumnName("OPER_SEQ_NBR");
        this.Property(e => e.WorkOrderNumber).HasColumnName("WORK_ORDER");
        this.Property(e => e.StartQuantity).HasColumnName("SCHED_COMP_QTY");
        this.Property(e => e.JobNumber).HasColumnName("JOB_NBR");
        this.Property(e => e.JobDetailId).HasColumnName("JOBDET_ID");
        this.Property(e => e.ComputerGeneratedNumber).HasColumnName("CPN");
        this.Property(e => e.ItemNumber).HasColumnName("ITEM_NBR");
        this.Property(e => e.ParentWorkOrder).HasColumnName("PARENT_WO");
        this.Property(e => e.ParentDueDate).HasColumnName("SCHED_COMP_DATE");
        this.Property(e => e.WorkOrderIssueDate).HasColumnName("RELEASE_DATE");
        this.Property(e => e.WorkInProgressOperationId).HasColumnName("WIPOPERACT_ID");
    }
}

        public class WorkInProgressOperationMapping : EntityTypeConfiguration<WorkInProgressOperation>
{

    public WorkInProgressOperationMapping()
    {
       this.ToTable("WIPOPER");
       this.HasKey(e => e.WorkInProgressOperationId).Ignore(e => e.EntityId);
       this.Property(e => e.WorkInProgressOperationId).HasColumnName("@ID");
       this.Property(e => e.OperationNumber).HasColumnName("OPERATION_NBR");
       this.Property(e => e.OperationSequence).HasColumnName("OPER_SEQ");
       this.Property(e => e.WorkOrder).HasColumnName("WORK_ORDER");
       this.Property(e => e.NextSequence).HasColumnName("NEXT_SEQ");
       this.Property(e => e.Status).HasColumnName("OPER_STATUS");
       this.Property(e => e.QuantityComplete).HasColumnName("QTY_COMPLETE");
       this.Property(e => e.SalesOrderDeliveryDate).HasColumnName("DUE_SO");
       this.Property(e => e.WorkOrderDeliveryDate).HasColumnName("WO_DUE");
       this.Property(e => e.StartingQuantity).HasColumnName("EXP_START_QTY");

    }
}

3 个答案:

答案 0 :(得分:0)

我能够在两个UniData表之间放置导航属性。我使用过Composite Key / Foreign Key。这是你的要求吗?

参见下面的示例。

我使用了以下内容:

U2数据库

  • UniData 7.3
  • 使用STUDENT_NF_SUB和STUDENT_CGA_MV_SUB表格的模拟账户
  • 模拟帐户使用VSG进行标准化

微软

  • Visual Studio 2012 Update 4
  • .NET Framework 4.5
  • Entity Framework 5.0(安装包实体框架-Version 5.0.0)
  • Code First Model
  • WinFrom App

服务器资源管理器中的UniData表

请参阅下面的Visual Studio 2012服务器浏览中的Unidata表。

SE.png

模型和DbContext代码

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test_EF5
{
    public class Student
    {
        public string StudentID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public virtual ICollection<StudentSemester> Semesters { get; set; }
    }

    public class StudentSemester
    {
        public string StudentID { get; set; }
        public string Semester { get; set; }
        public int CompositeKey { get; set; }
        public virtual Student Student { get; set; }
    }
    public class StudentMapping : EntityTypeConfiguration<Student>
    {
        public StudentMapping()
        {
            this.ToTable("STUDENT_NF_SUB");
            this.Property(e => e.StudentID).HasColumnName("ID");
            this.Property(e => e.FirstName).HasColumnName("FNAME");
            this.Property(e => e.LastName).HasColumnName("LNAME");
            this.HasKey(e => e.StudentID);
        }

    }

    public class StudentSemesterMapping : EntityTypeConfiguration<StudentSemester>
    {
        public StudentSemesterMapping()
        {
            this.ToTable("STUDENT_CGA_MV_SUB");
            this.Property(e => e.StudentID).HasColumnName("ID");
            this.Property(e => e.Semester).HasColumnName("SEMESTER");
            this.Property(e => e.CompositeKey).HasColumnName("CGA_MV_KEY");
            this.HasKey(e => new { e.StudentID, e.CompositeKey });

        }

    }
    public class StudentContext : DbContext
    {
        public StudentContext()
        {

        }
        public DbSet<Student> Students { get; set; }
        public DbSet<StudentSemester> StudentSemesters { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Configurations.Add(new StudentMapping());
            modelBuilder.Configurations.Add(new StudentSemesterMapping());

        }
    }
}

WinForm应用程序使用导航属性

调用代码优先
private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                Database.SetInitializer<StudentContext>(null);
                StudentContext ctx = new StudentContext();
                var r = ctx.Students.ToList();
                foreach (var item in r)
                {
                    this.textBox1.AppendText("ID="+item.StudentID+" FNAME=" + item.FirstName +" LNAME="+ item.LastName +Environment.NewLine);

                    foreach (var item2 in item.Semesters)
                    {
                        this.textBox1.AppendText("\t ID="+item2.StudentID + " Semester="+item2.Semester +" CompositeKey="+ item2.CompositeKey +Environment.NewLine);
                    }
                }
            }
            catch (Exception e4)
            {
                string lErr = e4.Message;
                this.textBox1.AppendText(lErr);
            }

        }

运行App

RunApp.png

答案 1 :(得分:0)

另一个例子:一对多关系(U2表/文件)

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test_EF5
{


    public class Customer
    {
        public string CustomerID { get; set; }
        public string FirstName { get; set; }

        public virtual ICollection<Rental> Rentals { get; set; }
    }

    public class Rental
    {
        public string RentalID { get; set; }
        public string CustomerID { get; set; }
        public decimal Balance { get; set; }
        public virtual Customer Customer { get; set; }
    }

    public class CustomerMapping : EntityTypeConfiguration<Customer>
    {
        public CustomerMapping()
        {
            this.ToTable("MEMBERS");
            this.Property(e => e.CustomerID).HasColumnName("MEMBERS_PK");
            this.Property(e => e.FirstName).HasColumnName("FIRST_NAME");
            this.HasKey(e => e.CustomerID);
        }

    }

    public class RentalMapping : EntityTypeConfiguration<Rental>
    {
        public RentalMapping()
        {
            this.ToTable("RENTAL_DETAILS");
            this.Property(e => e.RentalID).HasColumnName("RENTAL_DETAIL_PK");
            this.Property(e => e.Balance).HasColumnName("BALANCE.DUE");
            this.Property(e => e.CustomerID).HasColumnName("CUSTOMER.CODE");


            this.HasKey(e => new { e.RentalID });
            HasRequired(p => p.Customer)
                .WithMany(b => b.Rentals)
                .HasForeignKey(p => new { p.CustomerID });

        }

    }

    public class CustomerContext : DbContext
    {
        public CustomerContext()
        {

        }
        public DbSet<Customer> Customers { get; set; }
        public DbSet<Rental> Rentals { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Configurations.Add(new CustomerMapping());
            modelBuilder.Configurations.Add(new RentalMapping());

        }
    }
}

答案 2 :(得分:0)

使用Include()函数

进行预先加载
private void button3_Click(object sender, EventArgs e)
        {


            try
            {
                Database.SetInitializer<CustomerContext>(null);
                CustomerContext ctx = new CustomerContext();
                var r = ctx.Customers.Include("Rentals").ToList();
                foreach (var item in r)
                {
                    this.textBox1.AppendText("ID=" + item.CustomerID + " FNAME=" + item.FirstName + Environment.NewLine);
                    int k = 0;
                    foreach (var item2 in item.Rentals)
                    {
                        this.textBox1.AppendText("\t ID=" + item2.RentalID + " CustomerID=" + item2.CustomerID + " Balance=" + item2.Balance + Environment.NewLine);

                    }
                }

                int y = 0;
            }
            catch (Exception e4)
            {
                string lErr = e4.Message;
                this.textBox1.AppendText(lErr);
            }

        }