首页新闻找找看学习计划

ABP的Core2.0使用mysql数据,在使用linq语句报错了

0
悬赏园豆:180 [已解决问题] 解决于 2018-12-08 17:48

好像时mysql的bool类型与linq有问题 ,有谁有这样的错吗,

下面的错时在初始化数据的时候,在执行linq语句就报错了

No coercion operator is defined between types 'System.Int16' and 'System.Boo

有哪位大佬知道怎么解决吗?

 

 

 

 

 1 migrationBuilder.CreateTable(
 2                 name: "AbpEditions",
 3                 columns: table => new
 4                 {
 5                     Id = table.Column<int>(nullable: false)
 6                         .Annotation("MySQL:AutoIncrement", true),
 7                     CreationTime = table.Column<DateTime>(nullable: false),
 8                     CreatorUserId = table.Column<long>(nullable: true),
 9                     LastModificationTime = table.Column<DateTime>(nullable: true),
10                     LastModifierUserId = table.Column<long>(nullable: true),
11                     IsDeleted = table.Column<short>(nullable: false),
12                     DeleterUserId = table.Column<long>(nullable: true),
13                     DeletionTime = table.Column<DateTime>(nullable: true),
14                     Name = table.Column<string>(maxLength: 32, nullable: false),
15                     DisplayName = table.Column<string>(maxLength: 64, nullable: false)
16                 },
17                 constraints: table =>
18                 {
19                     table.PrimaryKey("PK_AbpEditions", x => x.Id);
20                 });
View Code
 1             modelBuilder.Entity("Abp.Application.Editions.Edition", b =>
 2                 {
 3                     b.Property<int>("Id")
 4                         .ValueGeneratedOnAdd();
 5 
 6                     b.Property<DateTime>("CreationTime");
 7 
 8                     b.Property<long?>("CreatorUserId");
 9 
10                     b.Property<long?>("DeleterUserId");
11 
12                     b.Property<DateTime?>("DeletionTime");
13 
14                     b.Property<string>("DisplayName")
15                         .IsRequired()
16                         .HasMaxLength(64);
17 
18                     b.Property<bool>("IsDeleted");
19 
20                     b.Property<DateTime?>("LastModificationTime");
21 
22                     b.Property<long?>("LastModifierUserId");
23 
24                     b.Property<string>("Name")
25                         .IsRequired()
26                         .HasMaxLength(32);
27 
28                     b.HasKey("Id");
29 
30                     b.ToTable("AbpEditions");
31                 });
View Code

执行下面的linq就报错了

 1  private void CreateEditions()
 2             {
 3                 var defaultEdition = _context.Editions.IgnoreQueryFilters().FirstOrDefault(e => e.Name == EditionManager.DefaultEditionName);
 4                 if (defaultEdition == null)
 5                 {
 6                     defaultEdition = new Edition { Name = EditionManager.DefaultEditionName, DisplayName = EditionManager.DefaultEditionName };
 7                     _context.Editions.Add(defaultEdition);
 8                     _context.SaveChanges();
 9 
10                     /* Add desired features to the standard edition, if wanted... */
11                 }
12             }
View Code

 

HANS许的主页 HANS许 | 初学一级 | 园豆:44
提问于:2018-11-24 19:46

建议提供一下EF Core的映射配置代码

dudu 11个月前

@dudu: 已附上 但是是项目里的所有bool类型都是这样子的 这个就是个例子

HANS许 11个月前
< >
分享
最佳答案
0

连接串: Data Source=IP;port=3306;Initial Catalog=hans;user id=root;password=***;charset=gbk;allow zero datetime=True;convert zero datetime=True;SslMode = none;

allow zero datetime=True;convert zero datetime=True; 就是这这个换换失败了 去掉就可以了

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

HANS许 | 初学一级 |园豆:44 | 2018-12-08 17:46
其他回答(2)
0

看一下你的实体定义,bit对应的是bool类型。

幻天芒 | 园豆:36522 (高人七级) | 2018-11-24 20:06

实体IsDelete也是bool类型

支持(0) 反对(0) HANS许 | 园豆:44 (初学一级) | 2018-11-24 20:10

@HANS许: 你这个实体怎么这么复杂呢~

支持(0) 反对(0) 幻天芒 | 园豆:36522 (高人七级) | 2018-11-25 01:45

@幻天芒:他这就就是采用了ABP的框架 

支持(0) 反对(0) HANS许 | 园豆:44 (初学一级) | 2018-11-25 12:24

@HANS许: 好吧,对这个不了解。

支持(0) 反对(0) 幻天芒 | 园豆:36522 (高人七级) | 2018-11-25 20:21
0

IsDeleted = table.Column<short>(nullable: false) 怎么是 short ?

收获园豆:180
dudu | 园豆:41322 (高人七级) | 2018-11-24 22:18

不懂,实用ef生成迁移文件的时候,他就是生成这样的。

支持(0) 反对(0) HANS许 | 园豆:44 (初学一级) | 2018-11-24 22:20

@HANS许: 参考 https://q.cnblogs.com/q/108459/

支持(0) 反对(0) dudu | 园豆:41322 (高人七级) | 2018-11-24 22:22

@dudu: 肯定是有找到这个帖子的,就是按照他这样子,加了连接字符串上去,还是不行

支持(0) 反对(0) HANS许 | 园豆:44 (初学一级) | 2018-11-24 22:23

@HANS许: 试试改为 b.Property<bool>("IsDeleted").HasColumnType("bit"); 重新生成数据库

支持(0) 反对(0) dudu | 园豆:41322 (高人七级) | 2018-11-24 22:26

@dudu:因为使用这个框架,他这个删除字段是继承类的,有很多的类,对象继承,有什么办法一次性解决,在ef这边直接解决掉吗?

支持(0) 反对(0) HANS许 | 园豆:44 (初学一级) | 2018-11-24 22:28

@HANS许: 就加个.HasColumnType("bit");,其他地方不用改

支持(0) 反对(0) dudu | 园豆:41322 (高人七级) | 2018-11-24 22:29

@dudu: 不是 我的意思是,这个IsDetele字段,是基类的字段,我很多字段继承他,但如你所见,他这边声场迁移文件就是short这样子,所以迁移文件有很多表示这样子的,有什么可以在更为上层去解决。

支持(0) 反对(0) HANS许 | 园豆:44 (初学一级) | 2018-11-24 22:32

@HANS许: 迁移文件不就是根据 DbContext 生成的吗?

支持(0) 反对(0) dudu | 园豆:41322 (高人七级) | 2018-11-24 22:45

@dudu: 的确,但是要怎么写呢?都是这边,Tenant, Role, User像这种也有“IsDelete”的字段,都是继承一个基类,总不能每个都去写“.HasColumnType("bit");”吧

 1 public class HANSCoreDbContext : AbpZeroDbContext<Tenant, Role, User, HANSCoreDbContext>
 2     {
 3         /* Define a DbSet for each entity of the application */
 4         
 5         public HANSCoreDbContext(DbContextOptions<HANSCoreDbContext> options)
 6             : base(options)
 7         {
 8            
 9         }
10 
11         protected override void OnModelCreating(ModelBuilder modelBuilder)
12         {
13             base.OnModelCreating(modelBuilder);
14 
15         }
16 
17       
18     }
HANSCoreDbContext
 1   //
 2     // 摘要:
 3     //     Base DbContext for ABP zero. Derive your DbContext from this class to have base
 4     //     entities.
 5     public abstract class AbpZeroDbContext<TTenant, TRole, TUser, TSelf> : AbpZeroCommonDbContext<TRole, TUser, TSelf>
 6         where TTenant : AbpTenant<TUser>
 7         where TRole : AbpRole<TUser>
 8         where TUser : AbpUser<TUser>
 9         where TSelf : AbpZeroDbContext<TTenant, TRole, TUser, TSelf>
10     {
11         //
12         // 参数:
13         //   options:
14         protected AbpZeroDbContext(DbContextOptions<TSelf> options);
15 
16         //
17         // 摘要:
18         //     Tenants
19         public virtual DbSet<TTenant> Tenants { get; set; }
20         //
21         // 摘要:
22         //     Editions.
23         public virtual DbSet<Edition> Editions { get; set; }
24         //
25         // 摘要:
26         //     FeatureSettings.
27         public virtual DbSet<FeatureSetting> FeatureSettings { get; set; }
28         //
29         // 摘要:
30         //     TenantFeatureSetting.
31         public virtual DbSet<TenantFeatureSetting> TenantFeatureSettings { get; set; }
32         //
33         // 摘要:
34         //     EditionFeatureSettings.
35         public virtual DbSet<EditionFeatureSetting> EditionFeatureSettings { get; set; }
36         //
37         // 摘要:
38         //     Background jobs.
39         public virtual DbSet<BackgroundJobInfo> BackgroundJobs { get; set; }
40         //
41         // 摘要:
42         //     User accounts
43         public virtual DbSet<UserAccount> UserAccounts { get; set; }
44         //
45         // 摘要:
46         //     Notifications.
47         public virtual DbSet<NotificationInfo> Notifications { get; set; }
48 
49         //
50         // 参数:
51         //   modelBuilder:
52         protected override void OnModelCreating(ModelBuilder modelBuilder);
53     }
AbpZeroDbContext

 

支持(0) 反对(0) HANS许 | 园豆:44 (初学一级) | 2018-11-24 22:59

@dudu: 按照这两个帖子来讲,官方的mysql,似乎并没有提供解决方案,其中有个人是使用了第三方的mysqlCoreEntityFrameWork。然后我也引用,是没有报错了。但是有引发一个错误了

System.InvalidOperationException
  HResult=0x80131509
  Message=An exception occurred while reading a database value for property 'Edition.CreationTime'. The expected type was 'System.DateTime' but the actual value was of type 'MySql.Data.Types.MySqlDateTime'.
  Source=Microsoft.EntityFrameworkCore
  StackTrace:
   at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityMaterializerSource.ThrowReadValueException[TValue](Exception exception, Object value, IPropertyBase property)
   at Microsoft.EntityFrameworkCore.Storage.Internal.TypedRelationalValueBufferFactory.Create(DbDataReader dataReader)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.BufferlessMoveNext(DbContext _, Boolean buffer)
   at Pomelo.EntityFrameworkCore.MySql.Storage.Internal.MySqlExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()
   at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Boolean& found)
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ResultEnumerable`1.GetEnumerator()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.<_TrackEntities>d__17`2.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
   at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Boolean& found)
   at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass15_1`1.<CompileQueryCore>b__0(QueryContext qc)
   at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source, Expression`1 predicate)
   at HANSCore.EntityFrameworkCore.Seed.Tenants.DefaultTenantBuilder.CreateDefaultTenant() in E:\HANS_x\Core\Web\HANSABPCore\HANSCore\4.2.1\aspnet-core\src\HANSCore.EntityFrameworkCore\EntityFrameworkCore\Seed\Tenants\DefaultTenantBuilder.cs:line 32
   at HANSCore.EntityFrameworkCore.Seed.Tenants.DefaultTenantBuilder.Create() in E:\HANS_x\Core\Web\HANSABPCore\HANSCore\4.2.1\aspnet-core\src\HANSCore.EntityFrameworkCore\EntityFrameworkCore\Seed\Tenants\DefaultTenantBuilder.cs:line 20
   at HANSCore.EntityFrameworkCore.Seed.SeedHelper.SeedHostDb(HANSCoreDbContext context) in E:\HANS_x\Core\Web\HANSABPCore\HANSCore\4.2.1\aspnet-core\src\HANSCore.EntityFrameworkCore\EntityFrameworkCore\Seed\SeedHelper.cs:line 28
   at HANSCore.EntityFrameworkCore.Seed.SeedHelper.WithDbContext[TDbContext](IIocResolver iocResolver, Action`1 contextAction) in E:\HANS_x\Core\Web\HANSABPCore\HANSCore\4.2.1\aspnet-core\src\HANSCore.EntityFrameworkCore\EntityFrameworkCore\Seed\SeedHelper.cs:line 41
   at HANSCore.EntityFrameworkCore.Seed.SeedHelper.SeedHostDb(IIocResolver iocResolver) in E:\HANS_x\Core\Web\HANSABPCore\HANSCore\4.2.1\aspnet-core\src\HANSCore.EntityFrameworkCore\EntityFrameworkCore\Seed\SeedHelper.cs:line 17
   at HANSCore.EntityFrameworkCore.HANSCoreEntityFrameworkModule.PostInitialize() in E:\HANS_x\Core\Web\HANSABPCore\HANSCore\4.2.1\aspnet-core\src\HANSCore.EntityFrameworkCore\EntityFrameworkCore\HANSCoreEntityFrameworkModule.cs:line 46
   at System.Collections.Generic.List`1.ForEach(Action`1 action)
   at Abp.AbpBootstrapper.Initialize()
   at Abp.AspNetCore.AbpApplicationBuilderExtensions.InitializeAbp(IApplicationBuilder app)
   at Abp.AspNetCore.AbpApplicationBuilderExtensions.UseAbp(IApplicationBuilder app, Action`1 optionsAction)
   at HANSCore.Web.Startup.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) in E:\HANS_x\Core\Web\HANSABPCore\HANSCore\4.2.1\aspnet-core\src\HANSCore.Web.Mvc\Startup\Startup.cs:line 54

内部异常 1:
InvalidCastException: Unable to cast object of type 'MySql.Data.Types.MySqlDateTime' to type 'System.DateTime'.
View Code

 

支持(0) 反对(0) HANS许 | 园豆:44 (初学一级) | 2018-11-24 23:55
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册