解决.NET 4.5与EF6 DatabaseGeneratedOption冲突问题

This commit is contained in:
yubaolee 2015-10-26 23:55:02 +08:00
parent 13d44f28b6
commit 885d444761
16 changed files with 156 additions and 41 deletions

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using OpenAuth.Domain;
using OpenAuth.Domain.Interface;
@ -18,5 +19,37 @@ namespace OpenAuth.App
{
return _repository.LoadOrgs();
}
public int AddOrg(Org org)
{
string cascadeId;
//根据同一级个数计算ID
int currentCascadeId = _repository.GetCount(o => o.ParentId == 0) + 1;
if (org.ParentId != 0)
{
var parentOrg = _repository.FindSingle(o => o.Id == org.ParentId);
if (parentOrg != null)
{
cascadeId = parentOrg.CascadeId +"." + currentCascadeId;
org.ParentName = parentOrg.Name;
}
else
{
throw new Exception("未能找到该组织的父节点信息");
}
}
else
{
cascadeId = "0." + currentCascadeId;
org.ParentName = "";
}
org.CascadeId = cascadeId;
org.CreateTime = DateTime.Now;
_repository.Add(org);
_repository.Save();
return org.Id;
}
}
}

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace OpenAuth.Domain.Interface
{
public interface IOrgRepository
public interface IOrgRepository :IRepository<Org>
{
IEnumerable<Org> LoadOrgs();
}

View File

@ -1,14 +1,14 @@
using System.Collections.Generic;
using System.Linq;
namespace OpenAuth.Domain.Interface
{
public interface IUserRepository
{
User FindByAccount(string account);
User FindById(string id);
IEnumerable<User> LoadUsers();
}
using System.Collections.Generic;
using System.Linq;
namespace OpenAuth.Domain.Interface
{
public interface IUserRepository :IRepository<User>
{
User FindByAccount(string account);
User FindById(string id);
IEnumerable<User> LoadUsers();
}
}

View File

@ -21,5 +21,13 @@ namespace OpenAuth.Domain
public System.DateTime CreateTime { get; set; }
public int CreateId { get; set; }
public int SortNo { get; set; }
public Org()
{
HotKey = "";
IconName = "";
BizCode = "";
CustomCode = "";
}
}
}

View File

@ -72,11 +72,11 @@ namespace <#= code.EscapeNamespace(efHost.Namespace) #>
{
if (isKey && storeGeneratedPattern != StoreGeneratedPattern.Identity)
{
configLines.Add(".HasDatabaseGeneratedOption(DatabaseGeneratedOption.None)");
// configLines.Add(".HasDatabaseGeneratedOption(DatabaseGeneratedOption.None)");
}
else if ((!isKey || efHost.EntityType.KeyMembers.Count > 1) && storeGeneratedPattern == StoreGeneratedPattern.Identity)
{
configLines.Add(".HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)");
// configLines.Add(".HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)");
}
}

View File

@ -12,8 +12,7 @@ namespace OpenAuth.Repository.Models.Mapping
this.HasKey(t => t.Id);
// Properties
this.Property(t => t.Id)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(t => t.Id);
// Table & Column Mappings
this.ToTable("ModuleRole");

View File

@ -12,8 +12,7 @@ namespace OpenAuth.Repository.Models.Mapping
this.HasKey(t => t.Id);
// Properties
this.Property(t => t.Id)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(t => t.Id);
// Table & Column Mappings
this.ToTable("PageElementGrant");

View File

@ -11,9 +11,6 @@ namespace OpenAuth.Repository.Models.Mapping
// Primary Key
this.HasKey(t => t.Id);
// Properties
this.Property(t => t.Id)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(t => t.DomId)
.IsRequired()

View File

@ -11,9 +11,6 @@ namespace OpenAuth.Repository.Models.Mapping
// Primary Key
this.HasKey(t => t.Id);
// Properties
this.Property(t => t.Id)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(t => t.Theme)
.IsRequired()

View File

@ -11,9 +11,6 @@ namespace OpenAuth.Repository.Models.Mapping
// Primary Key
this.HasKey(t => t.Id);
// Properties
this.Property(t => t.Id)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(t => t.Email)
.IsRequired()

View File

@ -11,9 +11,6 @@ namespace OpenAuth.Repository.Models.Mapping
// Primary Key
this.HasKey(t => t.Id);
// Properties
this.Property(t => t.Id)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(t => t.Account)
.IsRequired()

View File

@ -11,10 +11,6 @@ namespace OpenAuth.Repository.Models.Mapping
// Primary Key
this.HasKey(t => t.Id);
// Properties
this.Property(t => t.Id)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
// Table & Column Mappings
this.ToTable("UserModule");
this.Property(t => t.Id).HasColumnName("Id");

View File

@ -12,8 +12,7 @@ namespace OpenAuth.Repository.Models.Mapping
this.HasKey(t => t.Id);
// Properties
this.Property(t => t.Id)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(t => t.Id);
// Table & Column Mappings
this.ToTable("UserOrg");

View File

@ -12,8 +12,7 @@ namespace OpenAuth.Repository.Models.Mapping
this.HasKey(t => t.Id);
// Properties
this.Property(t => t.Id)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(t => t.Id);
// Table & Column Mappings
this.ToTable("UserRole");

View File

@ -66,6 +66,7 @@
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TestFunction.cs" />
<Compile Include="TestOrgApp.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />

View File

@ -0,0 +1,93 @@
using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenAuth.App;
using OpenAuth.Domain;
using OpenAuth.Repository;
namespace OpenAuth.UnitTest
{
/// <summary>
/// TestOrgApp 的摘要说明
/// </summary>
[TestClass]
public class TestOrgApp
{
public TestOrgApp()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
private TestContext testContextInstance;
private OrgManagerApp _app = new OrgManagerApp(new OrgRepository());
/// <summary>
///获取或设置测试上下文,该上下文提供
///有关当前测试运行及其功能的信息。
///</summary>
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region
//
// 编写测试时,可以使用以下附加特性:
//
// 在运行类中的第一个测试之前使用 ClassInitialize 运行代码
// [ClassInitialize()]
// public static void MyClassInitialize(TestContext testContext) { }
//
// 在类中的所有测试都已运行之后使用 ClassCleanup 运行代码
// [ClassCleanup()]
// public static void MyClassCleanup() { }
//
// 在运行每个测试之前,使用 TestInitialize 来运行代码
// [TestInitialize()]
// public void MyTestInitialize() { }
//
// 在每个测试运行完之后,使用 TestCleanup 来运行代码
// [TestCleanup()]
// public void MyTestCleanup() { }
//
#endregion
[TestMethod]
public void TestAddOrg()
{
int id = _app.AddOrg(new Org
{
Name = "集团总部",
ParentId = 0
});
Assert.IsTrue(id != 0);
id = _app.AddOrg(new Org
{
Name = "一分公司",
ParentId = id
});
Assert.IsTrue(id != 0);
}
[TestMethod]
public void TestLoadOrg()
{
var orgs = _app.GetAll();
foreach (var org in orgs)
{
Console.WriteLine(org.Name);
}
}
}
}