Created
May 10, 2020 15:30
-
-
Save avifatal/06190e3cd12c262f660d0a75a497e114 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Funq; | |
using ServiceStack; | |
using ServiceStack.Data; | |
using ServiceStack.DataAnnotations; | |
using ServiceStack.OrmLite; | |
using ServiceStack.Text; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Xunit; | |
namespace XUnitTestProject3 | |
{ | |
public class BaseEntity | |
{ | |
[AutoIncrement] | |
[PrimaryKey] | |
public long Id { get; set; } | |
} | |
public class ResearchEntity : BaseEntity | |
{ | |
[References(typeof(NameEntity))] | |
public long? PrimaryNameId { get; set; } | |
[Reference] | |
public NameEntity PrimaryName { get; set; } | |
[Reference] | |
public List<NameEntity> Names { get; set; } = new List<NameEntity>(); | |
} | |
public class NameEntity : BaseEntity | |
{ | |
public string Value { get; set; } | |
[References(typeof(ResearchEntity))] | |
public long ResearchId { get; set; } | |
[Reference] | |
public ResearchEntity Research { get; set; } | |
} | |
public class UnitTest1 | |
{ | |
[Fact] | |
public async Task Test1() | |
{ | |
var dbFactory = new OrmLiteConnectionFactory("Server=127.0.0.1;Database=db2;Port=3307;Uid=root;Pwd=root;", MySqlDialect.Provider); | |
using (var db = dbFactory.Open()) | |
{ | |
db.ExecuteSql("SET FOREIGN_KEY_CHECKS = 0;"); | |
db.DropAndCreateTable<NameEntity>(); | |
db.DropAndCreateTable<ResearchEntity>(); | |
db.ExecuteSql("SET FOREIGN_KEY_CHECKS = 1;"); | |
for (var i = 1; i <= 120; i++) | |
{ | |
var research = new ResearchEntity(); | |
research.Names.Add(new NameEntity { Value = "test 1" }); | |
research.Names.Add(new NameEntity { Value = "test 2" }); | |
research.Names.Add(new NameEntity { Value = "test 3" }); | |
research.Names.Add(new NameEntity { Value = "test 4" }); | |
research.Names.Add(new NameEntity { Value = "test 6" }); | |
research.Names.Add(new NameEntity { Value = "test 6" }); | |
db.Save(research, references: true); | |
research.PrimaryName = research.Names[1]; | |
db.Save(research, references: true); | |
} | |
} | |
using (var db = dbFactory.Open()) | |
{ | |
var res = (await db.LoadSelectAsync(db.From<ResearchEntity>().Where(x => x.Id == 5))).FirstNonDefault(); | |
Assert.Equal(res.PrimaryName.Id, res.PrimaryNameId); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment