Skip to content

Commit

Permalink
- 增加 FreeSql.DbContext OnModelCreating 虚方法,实现在 DbContext 使用 FluentApi;#4
Browse files Browse the repository at this point in the history
 - 移除 FreeSql.Extensions.EfCoreFluentApi,功能移至 FreeSql.DbContext;
  • Loading branch information
28810 authored and 28810 committed Apr 15, 2020
1 parent 43e1529 commit 3675940
Show file tree
Hide file tree
Showing 23 changed files with 418 additions and 575 deletions.
40 changes: 10 additions & 30 deletions Examples/dbcontext_01/Controllers/ValuesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ async public Task<string> Get()
repos2Song.Where(a => a.Id > 10).ToList();
//查询结果,进入 states

var song = new Song { };
var song = new Song { Title = "empty" };
repos2Song.Insert(song);
id = song.Id;

var adds = Enumerable.Range(0, 100)
.Select(a => new Song { Create_time = DateTime.Now, Is_deleted = false, Title = "xxxx" + a, Url = "url222" })
.Select(a => new Song { CreateTime = DateTime.Now, Title = "xxxx" + a, Url = "url222" })
.ToList();
//创建一堆无主键值

Expand All @@ -72,17 +72,7 @@ async public Task<string> Get()
var ctx = _songContext;
var tag = new Tag
{
Name = "testaddsublist",
Tags = new[] {
new Tag { Name = "sub1" },
new Tag { Name = "sub2" },
new Tag {
Name = "sub3",
Tags = new[] {
new Tag { Name = "sub3_01" }
}
}
}
Name = "testaddsublist"
};
ctx.Tags.Add(tag);

Expand All @@ -91,33 +81,23 @@ async public Task<string> Get()

var tagAsync = new Tag
{
Name = "testaddsublist",
Tags = new[] {
new Tag { Name = "sub1" },
new Tag { Name = "sub2" },
new Tag {
Name = "sub3",
Tags = new[] {
new Tag { Name = "sub3_01" }
}
}
}
Name = "testaddsublist"
};
await ctx.Tags.AddAsync(tagAsync);


ctx.Songs.Select.Where(a => a.Id > 10).ToList();
//查询结果,进入 states

song = new Song { };
song = new Song { Title = "empty" };
//可插入的 song

ctx.Songs.Add(song);
id = song.Id;
//因有自增类型,立即开启事务执行SQL,返回自增值

adds = Enumerable.Range(0, 100)
.Select(a => new Song { Create_time = DateTime.Now, Is_deleted = false, Title = "xxxx" + a, Url = "url222" })
.Select(a => new Song { CreateTime = DateTime.Now, Title = "xxxx" + a, Url = "url222" })
.ToList();
//创建一堆无主键值

Expand Down Expand Up @@ -159,12 +139,12 @@ async public Task<string> Get()
reposSong.Where(a => a.Id > 10).ToList();
//查询结果,进入 states

song = new Song { };
song = new Song { Title = "empty" };
reposSong.Insert(song);
id = song.Id;

adds = Enumerable.Range(0, 100)
.Select(a => new Song { Create_time = DateTime.Now, Is_deleted = false, Title = "xxxx" + a, Url = "url222" })
.Select(a => new Song { CreateTime = DateTime.Now, Title = "xxxx" + a, Url = "url222" })
.ToList();
//创建一堆无主键值

Expand Down Expand Up @@ -193,12 +173,12 @@ async public Task<string> Get()
using (ctx = new SongContext())
{

song = new Song { };
song = new Song { Title = "empty" };
await ctx.Songs.AddAsync(song);
id = song.Id;

adds = Enumerable.Range(0, 100)
.Select(a => new Song { Create_time = DateTime.Now, Is_deleted = false, Title = "xxxx" + a, Url = "url222" })
.Select(a => new Song { CreateTime = DateTime.Now, Title = "xxxx" + a, Url = "url222" })
.ToList();
await ctx.Songs.AddRangeAsync(adds);

Expand Down
87 changes: 70 additions & 17 deletions Examples/dbcontext_01/DbContexts/SongContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,109 @@

namespace dbcontext_01
{

public class SongContext : DbContext
{
public SongContext() : base() { }
public SongContext(IFreeSql fsql) : base(fsql, null) { }

public DbSet<Song> Songs { get; set; }
public DbSet<Tag> Tags { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder builder)
{
builder.UseFreeSql(Startup.Fsql);
//这里直接指定一个静态的 IFreeSql 对象即可,切勿重新 Build()
}

protected override void OnModelCreating(ICodeFirst codefirst)
{
codefirst.Entity<Song>(eb =>
{
eb.ToTable("tb_song");
eb.Ignore(a => a.Field1);
eb.Property(a => a.Title).HasColumnType("varchar(50)").IsRequired();
eb.Property(a => a.Url).HasMaxLength(100);
eb.Property(a => a.RowVersion).IsRowVersion();
eb.Property(a => a.CreateTime).HasDefaultValueSql("current_timestamp");
eb.HasKey(a => a.Id);
eb.HasIndex(a => new { a.Id, a.Title }).IsUnique().HasName("idx_xxx11");
//一对多、多对一
eb.HasOne(a => a.Type).HasForeignKey(a => a.TypeId).WithMany(a => a.Songs);
//多对多
eb.HasMany(a => a.Tags).WithMany(a => a.Songs, typeof(Song_tag));
});

codefirst.Entity<SongType>(eb =>
{
eb.HasMany(a => a.Songs).WithOne(a => a.Type).HasForeignKey(a => a.TypeId);
eb.HasData(new[]
{
new SongType
{
Id = 1,
Name = "流行",
Songs = new List<Song>(new[]
{
new Song{ Title = "真的爱你" },
new Song{ Title = "爱你一万年" },
})
},
new SongType
{
Id = 2,
Name = "乡村",
Songs = new List<Song>(new[]
{
new Song{ Title = "乡里乡亲" },
})
},
});
});

codefirst.SyncStructure<SongType>();
codefirst.SyncStructure<Song>();
}
}

public class SongType
{
public int Id { get; set; }
public string Name { get; set; }

public List<Song> Songs { get; set; }
}
public class Song
{
[Column(IsIdentity = true)]
public int Id { get; set; }
public DateTime? Create_time { get; set; }
public bool? Is_deleted { get; set; }
public string Title { get; set; }
public string Url { get; set; }
public DateTime CreateTime { get; set; }

public virtual ICollection<Tag> Tags { get; set; }
public int TypeId { get; set; }
public SongType Type { get; set; }
public List<Tag> Tags { get; set; }

[Column(IsVersion = true)]
public long versionRow { get; set; }
public int Field1 { get; set; }
public long RowVersion { get; set; }
}
public class Song_tag
{
public int Song_id { get; set; }
public virtual Song Song { get; set; }
public Song Song { get; set; }

public int Tag_id { get; set; }
public virtual Tag Tag { get; set; }
public Tag Tag { get; set; }
}

public class Tag
{
[Column(IsIdentity = true)]
public int Id { get; set; }
public int? Parent_id { get; set; }
public virtual Tag Parent { get; set; }

public decimal? Ddd { get; set; }
public string Name { get; set; }

public virtual ICollection<Song> Songs { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
public List<Song> Songs { get; set; }
}
}
49 changes: 0 additions & 49 deletions Examples/dbcontext_01/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,57 +16,8 @@ namespace dbcontext_01
{
public class Program
{

public class Song
{
[Column(IsIdentity = true)]
public int Id { get; set; }
public string BigNumber { get; set; }

[Column(IsVersion = true)]//使用简单
public long versionRow { get; set; }
}

public class SongContext : DbContext
{

public DbSet<Song> Songs { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder builder)
{
builder.UseFreeSql(fsql);
}
}
static IFreeSql fsql;
public static void Main(string[] args)
{
var asse = typeof(FreeSql.Internal.ObjectPool.ObjectPool<>).Assembly;

fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=|DataDirectory|\dd2.db;Pooling=true;Max Pool Size=10")
.UseAutoSyncStructure(true)
.UseLazyLoading(true)
.UseNoneCommandParameter(true)

.UseMonitorCommand(cmd => Trace.WriteLine(cmd.CommandText))
.Build();


using (var ctx = new SongContext())
{
var song = new Song { BigNumber = "1000000000000000000" };
ctx.Songs.Add(song);

ctx.Songs.Update(song);

song.BigNumber = (BigInteger.Parse(song.BigNumber) + 1).ToString();
ctx.Songs.Update(song);

ctx.SaveChanges();

var sql = fsql.Update<Song>().SetSource(song).ToSql();
}

CreateWebHostBuilder(args).Build().Run();
}

Expand Down
2 changes: 1 addition & 1 deletion Examples/efcore_to_freesql/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public Startup(IConfiguration configuration, ILoggerFactory loggerFactory)
.UseAutoSyncStructure(true)
.Build();

FreeSql.Extensions.EfCoreFluentApi.ICodeFirstExtensions.Test(Fsql);
FreeSqlDbContextExtensions.EfCoreFluentApiTest(Fsql);

DBContexts.BaseDBContext.Fsql = Fsql;

Expand Down
2 changes: 1 addition & 1 deletion Examples/efcore_to_freesql/efcore_to_freesql.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Extensions\FreeSql.Extensions.EfCoreFluentApi\FreeSql.Extensions.EfCoreFluentApi.csproj" />
<ProjectReference Include="..\..\FreeSql.DbContext\FreeSql.DbContext.csproj" />
<ProjectReference Include="..\..\FreeSql\FreeSql.csproj" />
<ProjectReference Include="..\..\Providers\FreeSql.Provider.Sqlite\FreeSql.Provider.Sqlite.csproj" />
</ItemGroup>
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 3675940

Please sign in to comment.