mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-08-23 22:11:35 +08:00
解决.NET 4.5与EF6 DatabaseGeneratedOption冲突问题
This commit is contained in:
parent
13d44f28b6
commit
885d444761
@ -1,4 +1,5 @@
|
|||||||
using System.Collections.Generic;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using OpenAuth.Domain;
|
using OpenAuth.Domain;
|
||||||
using OpenAuth.Domain.Interface;
|
using OpenAuth.Domain.Interface;
|
||||||
|
|
||||||
@ -18,5 +19,37 @@ namespace OpenAuth.App
|
|||||||
{
|
{
|
||||||
return _repository.LoadOrgs();
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace OpenAuth.Domain.Interface
|
namespace OpenAuth.Domain.Interface
|
||||||
{
|
{
|
||||||
public interface IOrgRepository
|
public interface IOrgRepository :IRepository<Org>
|
||||||
{
|
{
|
||||||
IEnumerable<Org> LoadOrgs();
|
IEnumerable<Org> LoadOrgs();
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
namespace OpenAuth.Domain.Interface
|
namespace OpenAuth.Domain.Interface
|
||||||
{
|
{
|
||||||
public interface IUserRepository
|
public interface IUserRepository :IRepository<User>
|
||||||
{
|
{
|
||||||
User FindByAccount(string account);
|
User FindByAccount(string account);
|
||||||
User FindById(string id);
|
User FindById(string id);
|
||||||
|
|
||||||
IEnumerable<User> LoadUsers();
|
IEnumerable<User> LoadUsers();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -21,5 +21,13 @@ namespace OpenAuth.Domain
|
|||||||
public System.DateTime CreateTime { get; set; }
|
public System.DateTime CreateTime { get; set; }
|
||||||
public int CreateId { get; set; }
|
public int CreateId { get; set; }
|
||||||
public int SortNo { get; set; }
|
public int SortNo { get; set; }
|
||||||
|
|
||||||
|
public Org()
|
||||||
|
{
|
||||||
|
HotKey = "";
|
||||||
|
IconName = "";
|
||||||
|
BizCode = "";
|
||||||
|
CustomCode = "";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -72,11 +72,11 @@ namespace <#= code.EscapeNamespace(efHost.Namespace) #>
|
|||||||
{
|
{
|
||||||
if (isKey && storeGeneratedPattern != StoreGeneratedPattern.Identity)
|
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)
|
else if ((!isKey || efHost.EntityType.KeyMembers.Count > 1) && storeGeneratedPattern == StoreGeneratedPattern.Identity)
|
||||||
{
|
{
|
||||||
configLines.Add(".HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)");
|
// configLines.Add(".HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,8 +12,7 @@ namespace OpenAuth.Repository.Models.Mapping
|
|||||||
this.HasKey(t => t.Id);
|
this.HasKey(t => t.Id);
|
||||||
|
|
||||||
// Properties
|
// Properties
|
||||||
this.Property(t => t.Id)
|
this.Property(t => t.Id);
|
||||||
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
|
|
||||||
|
|
||||||
// Table & Column Mappings
|
// Table & Column Mappings
|
||||||
this.ToTable("ModuleRole");
|
this.ToTable("ModuleRole");
|
||||||
|
@ -12,8 +12,7 @@ namespace OpenAuth.Repository.Models.Mapping
|
|||||||
this.HasKey(t => t.Id);
|
this.HasKey(t => t.Id);
|
||||||
|
|
||||||
// Properties
|
// Properties
|
||||||
this.Property(t => t.Id)
|
this.Property(t => t.Id);
|
||||||
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
|
|
||||||
|
|
||||||
// Table & Column Mappings
|
// Table & Column Mappings
|
||||||
this.ToTable("PageElementGrant");
|
this.ToTable("PageElementGrant");
|
||||||
|
@ -11,9 +11,6 @@ namespace OpenAuth.Repository.Models.Mapping
|
|||||||
// Primary Key
|
// Primary Key
|
||||||
this.HasKey(t => t.Id);
|
this.HasKey(t => t.Id);
|
||||||
|
|
||||||
// Properties
|
|
||||||
this.Property(t => t.Id)
|
|
||||||
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
|
|
||||||
|
|
||||||
this.Property(t => t.DomId)
|
this.Property(t => t.DomId)
|
||||||
.IsRequired()
|
.IsRequired()
|
||||||
|
@ -11,9 +11,6 @@ namespace OpenAuth.Repository.Models.Mapping
|
|||||||
// Primary Key
|
// Primary Key
|
||||||
this.HasKey(t => t.Id);
|
this.HasKey(t => t.Id);
|
||||||
|
|
||||||
// Properties
|
|
||||||
this.Property(t => t.Id)
|
|
||||||
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
|
|
||||||
|
|
||||||
this.Property(t => t.Theme)
|
this.Property(t => t.Theme)
|
||||||
.IsRequired()
|
.IsRequired()
|
||||||
|
@ -11,9 +11,6 @@ namespace OpenAuth.Repository.Models.Mapping
|
|||||||
// Primary Key
|
// Primary Key
|
||||||
this.HasKey(t => t.Id);
|
this.HasKey(t => t.Id);
|
||||||
|
|
||||||
// Properties
|
|
||||||
this.Property(t => t.Id)
|
|
||||||
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
|
|
||||||
|
|
||||||
this.Property(t => t.Email)
|
this.Property(t => t.Email)
|
||||||
.IsRequired()
|
.IsRequired()
|
||||||
|
@ -11,9 +11,6 @@ namespace OpenAuth.Repository.Models.Mapping
|
|||||||
// Primary Key
|
// Primary Key
|
||||||
this.HasKey(t => t.Id);
|
this.HasKey(t => t.Id);
|
||||||
|
|
||||||
// Properties
|
|
||||||
this.Property(t => t.Id)
|
|
||||||
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
|
|
||||||
|
|
||||||
this.Property(t => t.Account)
|
this.Property(t => t.Account)
|
||||||
.IsRequired()
|
.IsRequired()
|
||||||
|
@ -11,10 +11,6 @@ namespace OpenAuth.Repository.Models.Mapping
|
|||||||
// Primary Key
|
// Primary Key
|
||||||
this.HasKey(t => t.Id);
|
this.HasKey(t => t.Id);
|
||||||
|
|
||||||
// Properties
|
|
||||||
this.Property(t => t.Id)
|
|
||||||
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
|
|
||||||
|
|
||||||
// Table & Column Mappings
|
// Table & Column Mappings
|
||||||
this.ToTable("UserModule");
|
this.ToTable("UserModule");
|
||||||
this.Property(t => t.Id).HasColumnName("Id");
|
this.Property(t => t.Id).HasColumnName("Id");
|
||||||
|
@ -12,8 +12,7 @@ namespace OpenAuth.Repository.Models.Mapping
|
|||||||
this.HasKey(t => t.Id);
|
this.HasKey(t => t.Id);
|
||||||
|
|
||||||
// Properties
|
// Properties
|
||||||
this.Property(t => t.Id)
|
this.Property(t => t.Id);
|
||||||
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
|
|
||||||
|
|
||||||
// Table & Column Mappings
|
// Table & Column Mappings
|
||||||
this.ToTable("UserOrg");
|
this.ToTable("UserOrg");
|
||||||
|
@ -12,8 +12,7 @@ namespace OpenAuth.Repository.Models.Mapping
|
|||||||
this.HasKey(t => t.Id);
|
this.HasKey(t => t.Id);
|
||||||
|
|
||||||
// Properties
|
// Properties
|
||||||
this.Property(t => t.Id)
|
this.Property(t => t.Id);
|
||||||
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
|
|
||||||
|
|
||||||
// Table & Column Mappings
|
// Table & Column Mappings
|
||||||
this.ToTable("UserRole");
|
this.ToTable("UserRole");
|
||||||
|
@ -66,6 +66,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="TestFunction.cs" />
|
<Compile Include="TestFunction.cs" />
|
||||||
|
<Compile Include="TestOrgApp.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="App.config" />
|
<None Include="App.config" />
|
||||||
|
93
OpenAuth.UnitTest/TestOrgApp.cs
Normal file
93
OpenAuth.UnitTest/TestOrgApp.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user