Simple EF(Entity Framework) Demo

Step 1. pls install Entity Framework, then u can create a project(My project name: Jagre.Data).

Step 2. Pls add the item *.edmx to the project you created.

 

 

                                                                                    P1

 

Double click the file *.edmx and added some Entity Models. When you finish all entity Models, u can right click blank area and select highlight item.

 

 

P2

 

 

P3

Above operation will create two files : JagreDBModel.Context.tt, JagreDBModel.tt

 

Just a little modify *.tt files

 

JagreDBModel.Context.tt

This template file will generate Context class that implements the class DbContext. I think it main usage, as follows:

  1. According to connection string you specified to Connect to DB.
  2. Convert the table object of DB to the object DbSet.

 

The source code of this file

<#@ template language="C#" debug="false" hostspecific="true"#>

<#@ include file="EF.Utility.CS.ttinclude"#><#@

 output extension=".cs"#><#

 

var loader = new MetadataLoader(this);

var region = new CodeRegion(this);

var inputFile = @"JagreDB.edmx";

var ItemCollection = loader.CreateEdmItemCollection(inputFile);

 

Code = new CodeGenerationTools(this);

EFTools = new MetadataTools(this);

ObjectNamespace = Code.VsNamespaceSuggestion();

ModelNamespace = loader.GetModelNamespace(inputFile);

 

EntityContainer container = ItemCollection.GetItems<EntityContainer>().FirstOrDefault();

if (container == null)

{

    return string.Empty;

}

#>

//------------------------------------------------------------------------------

// <auto-generated>

// <#=GetResourceString("Template_GeneratedCodeCommentLine1")#>

//

// <#=GetResourceString("Template_GeneratedCodeCommentLine2")#>

// <#=GetResourceString("Template_GeneratedCodeCommentLine3")#>

// </auto-generated>

//------------------------------------------------------------------------------

 

<#

 

if (!String.IsNullOrEmpty(ObjectNamespace))

{

#>

/*Jagre, Modified Begin*/

namespace <#=Code.EscapeNamespace(ObjectNamespace)#>.DBContext

/*Jagre, Modified End*/

{

<#

    PushIndent(CodeRegion.GetIndent(1));

}

 

#>

using System;

using System.Data.Entity;

using System.Data.Entity.Infrastructure;

/*Jagre, Added Begin*/

using Jagre.Data.Entity;

/*Jagre, Added End*/

<#

if (container.FunctionImports.Any())

{

#>

using System.Data.Objects;

<#

}

#>

 

<#=Accessibility.ForType(container)#> partial class <#=Code.Escape(container)#> : DbContext

{

    public <#=Code.Escape(container)#>()

        : base("name=<#=container.Name#>")

    {

<#

        WriteLazyLoadingEnabled(container);

#>

    }

 

    protected override void OnModelCreating(DbModelBuilder modelBuilder)

    {

        throw new UnintentionalCodeFirstException();

    }

 

<#

    foreach (var entitySet in container.BaseEntitySets.OfType<EntitySet>())

    {

#>

    <#=Accessibility.ForReadOnlyProperty(entitySet)#> DbSet<<#=Code.Escape(entitySet.ElementType)#>> <#=Code.Escape(entitySet)#> { get; set; }

<#

    }

 

    foreach (var edmFunction in container.FunctionImports)

    {

        WriteFunctionImport(edmFunction, false);

    }

#>

}

<#

 

if (!String.IsNullOrEmpty(ObjectNamespace))

{

    PopIndent();

#>

}

<#

}

#>

<#+

string ModelNamespace { get; set; }

string ObjectNamespace { get; set; }

CodeGenerationTools Code { get; set; }

MetadataTools EFTools { get; set; }

 

string GetResourceString(string resourceName)

{

         if(_resourceManager == null)

         {

                   _resourceManager = new System.Resources.ResourceManager("System.Data.Entity.Design", typeof(System.Data.Entity.Design.MetadataItemCollectionFactory).Assembly);

         }

        

    return _resourceManager.GetString(resourceName, null);

}

System.Resources.ResourceManager _resourceManager;

 

void WriteLazyLoadingEnabled(EntityContainer container)

{

   string lazyLoadingAttributeValue = null;

   var lazyLoadingAttributeName = MetadataConstants.EDM_ANNOTATION_09_02 + ":LazyLoadingEnabled";

   if(MetadataTools.TryGetStringMetadataPropertySetting(container, lazyLoadingAttributeName, out lazyLoadingAttributeValue))

   {

       bool isLazyLoading;

       if(bool.TryParse(lazyLoadingAttributeValue, out isLazyLoading) && !isLazyLoading)

       {

#>

        this.Configuration.LazyLoadingEnabled = false;

<#+

       }

   }

}

 

void WriteFunctionImport(EdmFunction edmFunction, bool includeMergeOption)

{

    var parameters = FunctionImportParameter.Create(edmFunction.Parameters, Code, EFTools);

    var paramList = String.Join(", ", parameters.Select(p => p.FunctionParameterType + " " + p.FunctionParameterName).ToArray());

    var returnType = edmFunction.ReturnParameter == null ? null : EFTools.GetElementType(edmFunction.ReturnParameter.TypeUsage);

    var processedReturn = returnType == null ? "int" : "ObjectResult<" + MultiSchemaEscape(returnType) + ">";

 

    if (includeMergeOption)

    {

        paramList = Code.StringAfter(paramList, ", ") + "MergeOption mergeOption";

    }

#>

 

    <#=AccessibilityAndVirtual(Accessibility.ForMethod(edmFunction))#> <#=processedReturn#> <#=Code.Escape(edmFunction)#>(<#=paramList#>)

    {

<#+

        if(returnType != null && (returnType.EdmType.BuiltInTypeKind == BuiltInTypeKind.EntityType ||

                                  returnType.EdmType.BuiltInTypeKind == BuiltInTypeKind.ComplexType))

        {

#>

        ((IObjectContextAdapter)this).ObjectContext.MetadataWorkspace.LoadFromAssembly(typeof(<#=MultiSchemaEscape(returnType)#>).Assembly);

 

<#+

        }

 

        foreach (var parameter in parameters.Where(p => p.NeedsLocalVariable))

        {

            var isNotNull = parameter.IsNullableOfT ? parameter.FunctionParameterName + ".HasValue" : parameter.FunctionParameterName + " != null";

            var notNullInit = "new ObjectParameter(\"" + parameter.EsqlParameterName + "\", " + parameter.FunctionParameterName + ")";

            var nullInit = "new ObjectParameter(\"" + parameter.EsqlParameterName + "\", typeof(" + parameter.RawClrTypeName + "))";

#>

        var <#=parameter.LocalVariableName#> = <#=isNotNull#> ?

            <#=notNullInit#> :

            <#=nullInit#>;

 

<#+

        }

 

        var genericArg = returnType == null ? "" : "<" + MultiSchemaEscape(returnType) + ">";

        var callParams = Code.StringBefore(", ", String.Join(", ", parameters.Select(p => p.ExecuteParameterName).ToArray()));

 

        if (includeMergeOption)

        {

            callParams = ", mergeOption" + callParams;

        }

#>

        return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<#=genericArg#>("<#=edmFunction.Name#>"<#=callParams#>);

    }

<#+

    if(!includeMergeOption && returnType != null && returnType.EdmType.BuiltInTypeKind == BuiltInTypeKind.EntityType)

    {

        WriteFunctionImport(edmFunction, true);

    }

}

 

string AccessibilityAndVirtual(string accessibility)

{

    return accessibility + (accessibility != "private" ? " virtual" : "");

}

 

string MultiSchemaEscape(TypeUsage usage)

{

    var type = usage.EdmType as StructuralType;

    return type != null && type.NamespaceName != ModelNamespace ?

        Code.CreateFullName(Code.EscapeNamespace(type.NamespaceName), Code.Escape(type)) :

        Code.Escape(usage);

}

 

#>

 

 

 

 

 

 

JagreDBModel.tt

This template file will convert the concrete content of every table to the entity object.

 

The source code of this file

<#@ template language="C#" debug="false" hostspecific="true"#>

<#@ include file="EF.Utility.CS.ttinclude"#><#@

 output extension=".cs"#><#

 

CodeGenerationTools code = new CodeGenerationTools(this);

MetadataLoader loader = new MetadataLoader(this);

CodeRegion region = new CodeRegion(this, 1);

MetadataTools ef = new MetadataTools(this);

 

string inputFile = @"JagreDB.edmx";

EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);

/*Jagre, Modified Begin*/

string namespaceName = code.VsNamespaceSuggestion() + ".Entity";

/*Jagre, Modified End*/

EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);

WriteHeader(fileManager);

 

foreach (var entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))

{

    fileManager.StartNewFile(entity.Name + ".cs");

    BeginNamespace(namespaceName, code);

#>

using System;

using System.Collections.Generic;

/*Jagre, Added Begin*/

using System.Runtime.Serialization;

 

[Serializable]

[DataContract]

/*Jagre, Added End*/

<#=Accessibility.ForType(entity)#> <#=code.SpaceAfter(code.AbstractOption(entity))#>partial class <#=code.Escape(entity)#><#=code.StringBefore(" : ", code.Escape(entity.BaseType))#>

{

<#

    var propertiesWithDefaultValues = entity.Properties.Where(p => p.TypeUsage.EdmType is PrimitiveType && p.DeclaringType == entity && p.DefaultValue != null);

    var collectionNavigationProperties = entity.NavigationProperties.Where(np => np.DeclaringType == entity && np.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many);

    var complexProperties = entity.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == entity);

 

    if (propertiesWithDefaultValues.Any() || collectionNavigationProperties.Any() || complexProperties.Any())

    {

#>

    public <#=code.Escape(entity)#>()

    {

<#

        foreach (var edmProperty in propertiesWithDefaultValues)

        {

#>

        this.<#=code.Escape(edmProperty)#> = <#=code.CreateLiteral(edmProperty.DefaultValue)#>;

<#

        }

 

        foreach (var navigationProperty in collectionNavigationProperties)

        {

#>

        this.<#=code.Escape(navigationProperty)#> = new HashSet<<#=code.Escape(navigationProperty.ToEndMember.GetEntityType())#>>();

<#

        }

 

        foreach (var complexProperty in complexProperties)

        {

#>

        this.<#=code.Escape(complexProperty)#> = new <#=code.Escape(complexProperty.TypeUsage)#>();

<#

        }

#>

    }

 

<#

    }

 

    var primitiveProperties = entity.Properties.Where(p => p.TypeUsage.EdmType is PrimitiveType && p.DeclaringType == entity);

    if (primitiveProperties.Any())

    {

        foreach (var edmProperty in primitiveProperties)

        {

            WriteProperty(code, edmProperty);

        }

    }

 

    if (complexProperties.Any())

    {

#>

 

<#

        foreach(var complexProperty in complexProperties)

        {

            WriteProperty(code, complexProperty);

        }

    }

 

    var navigationProperties = entity.NavigationProperties.Where(np => np.DeclaringType == entity);

    if (navigationProperties.Any())

    {

#>

 

<#

        foreach (var navigationProperty in navigationProperties)

        {

            WriteNavigationProperty(code, navigationProperty);

        }

    }

#>

}

<#

    EndNamespace(namespaceName);

}

 

foreach (var complex in ItemCollection.GetItems<ComplexType>().OrderBy(e => e.Name))

{

    fileManager.StartNewFile(complex.Name + ".cs");

    BeginNamespace(namespaceName, code);

#>

using System;

 

<#=Accessibility.ForType(complex)#> partial class <#=code.Escape(complex)#>

{

<#

    var complexProperties = complex.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == complex);

    var propertiesWithDefaultValues = complex.Properties.Where(p => p.TypeUsage.EdmType is PrimitiveType && p.DeclaringType == complex && p.DefaultValue != null);

 

    if (propertiesWithDefaultValues.Any() || complexProperties.Any())

    {

#>

    public <#=code.Escape(complex)#>()

    {

<#

        foreach (var edmProperty in propertiesWithDefaultValues)

        {

#>

        this.<#=code.Escape(edmProperty)#> = <#=code.CreateLiteral(edmProperty.DefaultValue)#>;

<#

        }

 

        foreach (var complexProperty in complexProperties)

        {

#>

        this.<#=code.Escape(complexProperty)#> = new <#=code.Escape(complexProperty.TypeUsage)#>();

<#

        }

#>

    }

 

<#

    }

 

    var primitiveProperties = complex.Properties.Where(p => p.TypeUsage.EdmType is PrimitiveType && p.DeclaringType == complex);

    if (primitiveProperties.Any())

    {

        foreach(var edmProperty in primitiveProperties)

        {

            WriteProperty(code, edmProperty);

        }

    }

 

    if (complexProperties.Any())

    {

#>

 

<#

        foreach(var edmProperty in complexProperties)

        {

            WriteProperty(code, edmProperty);

        }

    }

#>

}

<#

    EndNamespace(namespaceName);

}

 

if (!VerifyTypesAreCaseInsensitiveUnique(ItemCollection))

{

    return "";

}

 

fileManager.Process();

 

#>

<#+

string GetResourceString(string resourceName)

{

         if(_resourceManager == null)

         {

                   _resourceManager = new System.Resources.ResourceManager("System.Data.Entity.Design", typeof(System.Data.Entity.Design.MetadataItemCollectionFactory).Assembly);

         }

        

    return _resourceManager.GetString(resourceName, null);

}

System.Resources.ResourceManager _resourceManager;

 

void WriteHeader(EntityFrameworkTemplateFileManager fileManager)

{

    fileManager.StartHeader();

#>

//------------------------------------------------------------------------------

// <auto-generated>

// <#=GetResourceString("Template_GeneratedCodeCommentLine1")#>

//

// <#=GetResourceString("Template_GeneratedCodeCommentLine2")#>

// <#=GetResourceString("Template_GeneratedCodeCommentLine3")#>

// </auto-generated>

//------------------------------------------------------------------------------

 

<#+

    fileManager.EndBlock();

}

 

void BeginNamespace(string namespaceName, CodeGenerationTools code)

{

    CodeRegion region = new CodeRegion(this);

    if (!String.IsNullOrEmpty(namespaceName))

    {

#>

namespace <#=code.EscapeNamespace(namespaceName)#>

{

<#+

        PushIndent(CodeRegion.GetIndent(1));

    }

}

 

 

void EndNamespace(string namespaceName)

{

    if (!String.IsNullOrEmpty(namespaceName))

    {

        PopIndent();

#>

}

<#+

    }

}

 

void WriteProperty(CodeGenerationTools code, EdmProperty edmProperty)

{

    WriteProperty(Accessibility.ForProperty(edmProperty),

                  code.Escape(edmProperty.TypeUsage),

                  code.Escape(edmProperty),

                  code.SpaceAfter(Accessibility.ForGetter(edmProperty)),

                  code.SpaceAfter(Accessibility.ForSetter(edmProperty)));

}

 

void WriteNavigationProperty(CodeGenerationTools code, NavigationProperty navigationProperty)

{

    var endType = code.Escape(navigationProperty.ToEndMember.GetEntityType());

    WriteProperty(PropertyVirtualModifier(Accessibility.ForProperty(navigationProperty)),

                  navigationProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many ? ("ICollection<" + endType + ">") : endType,

                  code.Escape(navigationProperty),

                  code.SpaceAfter(Accessibility.ForGetter(navigationProperty)),

                  code.SpaceAfter(Accessibility.ForSetter(navigationProperty)));

}

/*Jagre, this method is very important. Somewhere will invoke it. It will write properties of the Entity model*/

void WriteProperty(string accessibility, string type, string name, string getterAccessibility, string setterAccessibility)

{

#>

         /*Jagre, Modified Begin*/

         ///<sumary>

         /// 獲取或設置<#=name#>

         ///</sumary>

         [DataMember]

         <#=accessibility#> <#=type#> <#=name#>

         {

                   <#=getterAccessibility#>get;

                   <#=setterAccessibility#>set;

         }

/*Jagre, Modefied End*/

<#+

}

 

string PropertyVirtualModifier(string accessibility)

{

    return accessibility + (accessibility != "private" ? " virtual" : "");

}

 

bool VerifyTypesAreCaseInsensitiveUnique(EdmItemCollection itemCollection)

{

    var alreadySeen = new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase);

    foreach(var type in itemCollection.GetItems<StructuralType>())

    {

        if (!(type is EntityType || type is ComplexType))

        {

            continue;

        }

 

        if (alreadySeen.ContainsKey(type.FullName))

        {

            Error(String.Format(CultureInfo.CurrentCulture, "This template does not support types that differ only by case, the types {0} are not supported", type.FullName));

            return false;

        }

        else

        {

            alreadySeen.Add(type.FullName, true);

        }

    }

 

    return true;

}

#>

 

 

 

According to current entity model to create DB. Pls Select highlight item.

 

P4

P5

 

 

Summarize:

  1. Red source code was modified by myself. In fact, should delete my remark
  2. When u press “Ctrl + S” after modification the template file, the VS will auto generate *.cs files
  3. Above all steps will construct the Entity Models, if you wanna change the format of the entity class, u might modify your *.tt files
  4. Wish it useful for you.

转载于:https://www.cnblogs.com/Jagre/archive/2012/08/28/EF_Simple_Demo.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Entity Framework是微软开发的一种对象关系映射(ORM)框架,用于在.NET应用程序中与数据库进行交互。它提供了一种简化和自动化数据库操作的方式,使开发人员能够使用面向对象的方式来处理数据库。 在上述引用中,是一个示例代码,展示了如何创建一个继承自DbContext的自定义数据库上下文类MyDBContext,并定义了一个DbSet属性Persons,用于表示数据库中的Person表。此外,还重写了OnModelCreating方法,用于配置数据库模型。 是关于Entity Framework的一些开发团队的建议。他们强烈建议使用using语句来确保DbContext实例的正确释放,以避免潜在的资源泄漏问题。同时,他们指出,仅仅为了利用延迟加载而不使用using语句是违反分层原则的。 以下是一个简单的示例,演示了如何使用Entity Framework进行数据库操作: ```csharp using (var context = new MyDBContext()) { // 查询所有的Person记录 var persons = context.Persons.ToList(); // 添加新的Person记录 var newPerson = new Person { Name = "John", Age = 25 }; context.Persons.Add(newPerson); context.SaveChanges(); // 更新Person记录 var personToUpdate = context.Persons.FirstOrDefault(p => p.Name == "John"); if (personToUpdate != null) { personToUpdate.Age = 30; context.SaveChanges(); } // 删除Person记录 var personToDelete = context.Persons.FirstOrDefault(p => p.Name == "John"); if (personToDelete != null) { context.Persons.Remove(personToDelete); context.SaveChanges(); } } ``` 这个示例展示了如何使用Entity Framework的DbContext类来查询、添加、更新和删除数据库中的Person记录。使用using语句确保DbContext实例在使用完毕后被正确释放。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值