如何对实体框架类进行反射(模型优先方法)



当使用DataBase First时,可以创建为VisualStudio生成的类的实例?

我的意思是,我使用t4模板和命令"生成代码"使用t4模板的MVC3工具更新来创建我的类从db多对多的关系。

我可以使用反射在不同的程序集(dll)中"填充"该类的实例的属性吗?

以3个表为例:"课程"学生"studentsCourses"

我将有3个实体,一个用于课程,一个用于学生,一个用于关系。我可以接收一个对象或类型"学生",并使用反射来"填充"它的所有属性?如果是,我该怎么做呢?

我已经知道我可以通过引用接收对象作为"对象类型"(使用泛型),但我只是不知道如何创建/填充复杂的属性(例如在studentcourses属性)

有任何建议,请提前感谢。

这里我让脚本创建表和数据库"COURSES_TEST":

USE [master]
GO
/****** Object:  Database [COURSES_TEST]    Script Date: 04/12/2013 23:47:18 ******/
CREATE DATABASE [COURSES_TEST] ON  PRIMARY 
( NAME = N'COURSES_TEST', FILENAME = N'C:Program FilesMicrosoft SQL ServerMSSQL10.SQLEXPRESSMSSQLDATACOURSES_TEST.mdf' , SIZE = 2048KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON 
( NAME = N'COURSES_TEST_log', FILENAME = N'C:Program FilesMicrosoft SQL ServerMSSQL10.SQLEXPRESSMSSQLDATACOURSES_TEST_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO
ALTER DATABASE [COURSES_TEST] SET COMPATIBILITY_LEVEL = 100
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [COURSES_TEST].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO
ALTER DATABASE [COURSES_TEST] SET ANSI_NULL_DEFAULT OFF
GO
ALTER DATABASE [COURSES_TEST] SET ANSI_NULLS OFF
GO
ALTER DATABASE [COURSES_TEST] SET ANSI_PADDING OFF
GO
ALTER DATABASE [COURSES_TEST] SET ANSI_WARNINGS OFF
GO
ALTER DATABASE [COURSES_TEST] SET ARITHABORT OFF
GO
ALTER DATABASE [COURSES_TEST] SET AUTO_CLOSE OFF
GO
ALTER DATABASE [COURSES_TEST] SET AUTO_CREATE_STATISTICS ON
GO
ALTER DATABASE [COURSES_TEST] SET AUTO_SHRINK OFF
GO
ALTER DATABASE [COURSES_TEST] SET AUTO_UPDATE_STATISTICS ON
GO
ALTER DATABASE [COURSES_TEST] SET CURSOR_CLOSE_ON_COMMIT OFF
GO
ALTER DATABASE [COURSES_TEST] SET CURSOR_DEFAULT  GLOBAL
GO
ALTER DATABASE [COURSES_TEST] SET CONCAT_NULL_YIELDS_NULL OFF
GO
ALTER DATABASE [COURSES_TEST] SET NUMERIC_ROUNDABORT OFF
GO
ALTER DATABASE [COURSES_TEST] SET QUOTED_IDENTIFIER OFF
GO
ALTER DATABASE [COURSES_TEST] SET RECURSIVE_TRIGGERS OFF
GO
ALTER DATABASE [COURSES_TEST] SET  DISABLE_BROKER
GO
ALTER DATABASE [COURSES_TEST] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
GO
ALTER DATABASE [COURSES_TEST] SET DATE_CORRELATION_OPTIMIZATION OFF
GO
ALTER DATABASE [COURSES_TEST] SET TRUSTWORTHY OFF
GO
ALTER DATABASE [COURSES_TEST] SET ALLOW_SNAPSHOT_ISOLATION OFF
GO
ALTER DATABASE [COURSES_TEST] SET PARAMETERIZATION SIMPLE
GO
ALTER DATABASE [COURSES_TEST] SET READ_COMMITTED_SNAPSHOT OFF
GO
ALTER DATABASE [COURSES_TEST] SET HONOR_BROKER_PRIORITY OFF
GO
ALTER DATABASE [COURSES_TEST] SET  READ_WRITE
GO
ALTER DATABASE [COURSES_TEST] SET RECOVERY SIMPLE
GO
ALTER DATABASE [COURSES_TEST] SET  MULTI_USER
GO
ALTER DATABASE [COURSES_TEST] SET PAGE_VERIFY CHECKSUM
GO
ALTER DATABASE [COURSES_TEST] SET DB_CHAINING OFF
GO
USE [COURSES_TEST]
GO
/****** Object:  Table [dbo].[students]    Script Date: 04/12/2013 23:47:19 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[students](
    [studentID] [int] IDENTITY(1,1) NOT NULL,
    [name] [nvarchar](50) NULL,
    [lastName] [nvarchar](50) NULL,
 CONSTRAINT [PK_students] PRIMARY KEY CLUSTERED 
(
    [studentID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[courses]    Script Date: 04/12/2013 23:47:19 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[courses](
    [courseID] [int] IDENTITY(1,1) NOT NULL,
    [course] [nchar](10) NULL,
    [description] [nvarchar](50) NULL,
 CONSTRAINT [PK_courses] PRIMARY KEY CLUSTERED 
(
    [courseID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[Studentscourses]    Script Date: 04/12/2013 23:47:19 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Studentscourses](
    [courseStudentID] [int] IDENTITY(1,1) NOT NULL,
    [courseID] [int] NULL,
    [studentID] [int] NULL,
 CONSTRAINT [PK_Studentscourses] PRIMARY KEY CLUSTERED 
(
    [courseStudentID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  ForeignKey [FK_Studentscourses_courses]    Script Date: 04/12/2013 23:47:19 ******/
ALTER TABLE [dbo].[Studentscourses]  WITH CHECK ADD  CONSTRAINT [FK_Studentscourses_courses] FOREIGN KEY([courseID])
REFERENCES [dbo].[courses] ([courseID])
GO
ALTER TABLE [dbo].[Studentscourses] CHECK CONSTRAINT [FK_Studentscourses_courses]
GO
/****** Object:  ForeignKey [FK_Studentscourses_students]    Script Date: 04/12/2013 23:47:19 ******/
ALTER TABLE [dbo].[Studentscourses]  WITH CHECK ADD  CONSTRAINT [FK_Studentscourses_students] FOREIGN KEY([studentID])
REFERENCES [dbo].[students] ([studentID])
GO
ALTER TABLE [dbo].[Studentscourses] CHECK CONSTRAINT [FK_Studentscourses_students]
GO

如果我没理解错的话,EF会生成类似

的东西
class YourStudent
{
    public int studentId { get; set; }
    public string name { get; set; }
    public string lastName { get; set; }
    public virtual ICollection<Course> Courses { get; set; }
}

class TheirStudent
{
    public int studentId {get;set;}
    public string name { get; set; }
    public string lastName { get; set; }
}

,而你想把TheirStudent实例中的值塞进YourStudent实例。

比如

YourStudent s1;
TheirStudent s2;
Type t = typeof(YourStudent);
Type t2 = typeof(TheirStudent);
foreach (PropertyInfo pinfo in t.GetProperties()) 
{
    if (pinfo.GetGetMethod().IsVirtual &&
        pinfo.PropertyType.IsGenericType &&
        pinfo.PropertyType.GetGenericTypeDefinition() == typeof(ICollection<>))
    {
        // This is [probably] a nav property. Courses would be in here
    }
    else if (pinfo.GetGetMethod().IsVirtual)
    {
        // if you have 0..1 or 1..1 nav properties you might muck around in here
    }
    else if (!pinfo.GetGetMethod().IsVirtual)
    {
        // This is [probably] a column - studentId, name, etc would be here
        // you could pull values over like so:
        PropertyInfo pinfo2 = (from ps in t2.GetProperties() 
                               where ps.Name == pinfo.Name 
                               select ps).SingleOrDefault();
        if (pinfo2 != null)
        {
            object value = pinfo2.GetValue(s2, new object[] { });
            pinfo.SetValue(s1, value, new object[] { });
        }
    }
}

相关内容

  • 没有找到相关文章