日期实体类文件和多个模式

时间:2016-04-14 15:07:47

标签: c# sql-server entity-framework schema

场景:
具有多个架构的数据库,例如[DBO],[S1],[S2]。

模式可能包含一个与另一个模式中的表同名的表,但它们可能不一定具有相同的定义,因此[dbo]。[table1]可能与[s1]相同或不同[表1]。

我创建了一个生成类文件的应用。例如,这里是为[dbo]生成的类文件。[StudentRemote]进行了一些修改以隐藏一些加密信息:

using System;
using System.ComponentModel.DataAnnotations;
using System.Security.Cryptography;

namespace FlexIT.Model
{
    namespace dbo
    {
        public class StudentRemote
        {
            private Guid _id = Guid.NewGuid();

            public StudentRemote()
            {
                RecordTimeStamp = null;
                WeeklyMeetingTime = null;
                WeeklyMeetingDay = null;
                CWUCatalogNumber = null;
                Salt = null;
                RawApplicationText = null;
                Status = null;
                BreakupDate = null;
                MentorAssignedDate = null;
                MentorID = null;
                EmploymentHoursPerWeek = null;
                TransferDegreeDate = null;
                TransferDegreeInstitution = null;
                TransferDegree = null;
                CompleteBy = null;
                StartDate = null;
                AcceptDate = null;
                ApplicationDate = null;
                RecordCreateDate = null;
                FirstContactLogID = null;
                Resume = null;
                CoverLetter = null;
                CWUID = null;
                Zip = null;
                State = null;
                City = null;
                Address2 = null;
                Address1 = null;
                PhoneNumber = null;
                EmailAddressCWU = null;
                EmailAddressPersonal = null;
                Position = null;
                Employer = null;
                Gender = null;
                Birthdate = null;
                LastName = null;
                MI = null;
                FirstName = null;
            }

            [Mapping(ColumnName = "ID", IsUpdateable = true, IsIdentity = false)]
            [Key]
            public Guid ID
            {
                get { return _id; }
                set { _id = value; }
            }

            [Mapping(ColumnName = "FirstName", IsUpdateable = true, IsIdentity = false,Salt="one")]
            public String FirstName { get; set; }

            [Mapping(ColumnName = "MI", IsUpdateable = true, IsIdentity = false,Salt="two")]
            public String MI { get; set; }

            [Mapping(ColumnName = "LastName", IsUpdateable = true, IsIdentity = false,Salt="three")]
            public String LastName { get; set; }

            [Mapping(ColumnName = "Birthdate", IsUpdateable = true, IsIdentity = false)]
            public DateTime? Birthdate { get; set; }

            [Mapping(ColumnName = "Gender", IsUpdateable = true, IsIdentity = false)]
            public String Gender { get; set; }

            [Mapping(ColumnName = "Employer", IsUpdateable = true, IsIdentity = false)]
            public String Employer { get; set; }

            [Mapping(ColumnName = "Position", IsUpdateable = true, IsIdentity = false)]
            public String Position { get; set; }

            [Mapping(ColumnName = "EmailAddressPersonal", IsUpdateable = true, IsIdentity = false)]
            public String EmailAddressPersonal { get; set; }

            [Mapping(ColumnName = "EmailAddressCWU", IsUpdateable = true, IsIdentity = false)]
            public String EmailAddressCWU { get; set; }

            [Mapping(ColumnName = "PhoneNumber", IsUpdateable = true, IsIdentity = false)]
            public String PhoneNumber { get; set; }

            [Mapping(ColumnName = "Address1", IsUpdateable = true, IsIdentity = false)]
            public String Address1 { get; set; }

            [Mapping(ColumnName = "Address2", IsUpdateable = true, IsIdentity = false)]
            public String Address2 { get; set; }

            [Mapping(ColumnName = "City", IsUpdateable = true, IsIdentity = false)]
            public String City { get; set; }

            [Mapping(ColumnName = "State", IsUpdateable = true, IsIdentity = false)]
            public String State { get; set; }

            [Mapping(ColumnName = "Zip", IsUpdateable = true, IsIdentity = false)]
            public String Zip { get; set; }

            [Mapping(ColumnName = "CWUID", IsUpdateable = true, IsIdentity = false)]
            public String CWUID { get; set; }

            [Mapping(ColumnName = "CoverLetter", IsUpdateable = true, IsIdentity = false)]
            public String CoverLetter { get; set; }

            [Mapping(ColumnName = "Resume", IsUpdateable = true, IsIdentity = false)]
            public String Resume { get; set; }

            [Mapping(ColumnName = "FirstContactLogID", IsUpdateable = true, IsIdentity = false)]
            public Int32? FirstContactLogID { get; set; }

            [Mapping(ColumnName = "RecordCreateDate", IsUpdateable = true, IsIdentity = false)]
            public DateTime? RecordCreateDate { get; set; }

            [Mapping(ColumnName = "ApplicationDate", IsUpdateable = true, IsIdentity = false)]
            public DateTime? ApplicationDate { get; set; }

            [Mapping(ColumnName = "AcceptDate", IsUpdateable = true, IsIdentity = false)]
            public DateTime? AcceptDate { get; set; }

            [Mapping(ColumnName = "StartDate", IsUpdateable = true, IsIdentity = false)]
            public DateTime? StartDate { get; set; }

            [Mapping(ColumnName = "CompleteBy", IsUpdateable = true, IsIdentity = false)]
            public DateTime? CompleteBy { get; set; }

            [Mapping(ColumnName = "TransferDegree", IsUpdateable = true, IsIdentity = false)]
            public String TransferDegree { get; set; }

            [Mapping(ColumnName = "TransferDegreeInstitution", IsUpdateable = true, IsIdentity = false)]
            public String TransferDegreeInstitution { get; set; }

            [Mapping(ColumnName = "TransferDegreeDate", IsUpdateable = true, IsIdentity = false)]
            public DateTime? TransferDegreeDate { get; set; }

            [Mapping(ColumnName = "EmploymentHoursPerWeek", IsUpdateable = true, IsIdentity = false)]
            public Int32? EmploymentHoursPerWeek { get; set; }

            [Mapping(ColumnName = "MentorID", IsUpdateable = true, IsIdentity = false)]
            public Guid? MentorID { get; set; }

            [Mapping(ColumnName = "MentorAssignedDate", IsUpdateable = true, IsIdentity = false)]
            public DateTime? MentorAssignedDate { get; set; }

            [Mapping(ColumnName = "BreakupDate", IsUpdateable = true, IsIdentity = false)]
            public DateTime? BreakupDate { get; set; }

            [Mapping(ColumnName = "Status", IsUpdateable = true, IsIdentity = false)]
            public Int32? Status { get; set; }

            [Mapping(ColumnName = "RawApplicationText", IsUpdateable = true, IsIdentity = false)]
            public String RawApplicationText { get; set; }

            [Mapping(ColumnName = "Salt", IsUpdateable = true, IsIdentity = false)]
            public String Salt { get; set; }

            [Mapping(ColumnName = "CWUCatalogNumber", IsUpdateable = true, IsIdentity = false)]
            public String CWUCatalogNumber { get; set; }

            [Mapping(ColumnName = "WeeklyMeetingDay", IsUpdateable = true, IsIdentity = false)]
            public String WeeklyMeetingDay { get; set; }

            [Mapping(ColumnName = "WeeklyMeetingTime", IsUpdateable = true, IsIdentity = false)]
            public DateTime? WeeklyMeetingTime { get; set; }

            [Mapping(ColumnName = "RecordTimeStamp", IsUpdateable = true, IsIdentity = false)]
            public Byte[] RecordTimeStamp { get; set; }
        }

        public class StudentRemoteController : Controller<StudentRemote>
        {
            // Create generic controller at runttime
            public StudentRemoteController() : base("StudentRemote", "ID", "table", "dbo")
            {
            }
            // Create custom methods here
            // Use "public new" to completely override a method in the generic controller
        }
    }
}

正如您所看到的,我有一个主命名空间 - FlexIT.Model - 和一个&#34;子命名空间&#34;这是表实体的模式。

因此...

在我调用此实体模型的应用程序中,我执行以下操作。 (此代码查询[dbo]。[StudentRemote]并解密名字。)

您可以在我的使用陈述中看到我尝试做的以及我是如何做的。

我的问题 - 是否有更好的方法来处理同一实体模型中的多个模式?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using fm=FlexIT.Model;
using fmdbo=FlexIT.Model.dbo;
using fmtemp=FlexIT.Model.temp;
using clsCrypto;
using System.ComponentModel;

namespace FlexIT
{
    class Program
    {
        static public string FlexITKey = "FakeKeyForSecurityPurposes";
        static void Main(string[] args)
        {
            var src = new fm.dbo.StudentRemoteController();
            // or...
            // var src = fmdbo.StudentRemoteController();
            // or, if we were querying [temp].[StudentRemote]...
            // var src = fm.temp.StudentRemoteController();
            // or...
            // var src = fmtemp.StudentRemoteController();
            //
            src.Gets(pKeyname: "ID", pKeyval:new Guid(), pWhere:"or id is not null");

            foreach (var sr in src.InsertRec)
            {
                foreach (var prop in typeof(fm.dbo.StudentRemote).GetProperties())
                {
                    AttributeCollection attributes = TypeDescriptor.GetProperties(typeof(fmdbo.StudentRemote))[prop.Name].Attributes;
                    fm.MappingAttribute myMappings = (fm.MappingAttribute)attributes[typeof(fm.MappingAttribute)];
                    if (myMappings.Salt != null)
                    {
                        prop.SetValue(sr, DecryptIt(prop.GetValue(sr, null).ToString(), myMappings.Salt, sr.Salt));
                    }
                }
                Console.WriteLine(sr.FirstName);
            }          
        }

        public static string DecryptIt(string encryptedString, string additionalSalt = "", string recordSalt = "")
        {
            //s=encrypted string
            //e=additional salt
            //h=record salt if needed. This is usually the student's salt value


            CryptoFilterBox mycrypto = new CryptoFilterBox();

            if (String.IsNullOrEmpty(encryptedString) || encryptedString.Trim().Length == 0)
            {
                return encryptedString;
            }
            mycrypto.set_InBuffer(encryptedString.Replace("''", "'"));
            mycrypto.set_Password(FlexITKey + (String.IsNullOrEmpty(recordSalt) ? "" : recordSalt) + additionalSalt);
            mycrypto.Decrypt();
            return mycrypto.OutBuffer;
        }

    }

}

0 个答案:

没有答案