请问关于c的具体应用场景有哪些?

2026-04-29 03:452阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计341个文字,预计阅读时间需要2分钟。

请问关于c的具体应用场景有哪些?

在OnModelCreating中尝试将枚举映射到smallint时,遇到以下异常:InvalidCastException: Unable to cast object of type 'System.Byte' to type 'System.Int32'. 这可能是由于在SQL Server中,int类型占用4个字节,而smallint只占用2个字节。

当我尝试在OnModelCreating中将枚举映射到smallint时,我得到以下异常:

请问关于c的具体应用场景有哪些?

InvalidCastException: Unable to cast object of type ‘System.Byte’ to type ‘System.Int32’.

我想这样做是因为在SQL Server中,int是4个字节而tinyint是1个字节.

相关代码:
实体:

namespace SOMapping.Data { public class Tag { public int Id { get; set; } public TagType TagType { get; set; } } public enum TagType { Foo, Bar } }

的DbContext:

using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; namespace SOMapping.Data { public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } public DbSet<Tag> Tags { get; set; } protected override void OnModelCreating(ModelBuilder builder) { builder.Entity<Tag>().Property(m => m.TagType).HasColumnType("smallint"); base.OnModelCreating(builder); } } }

查询:

using System.Linq; using Microsoft.AspNetCore.Mvc; using SOMapping.Data; namespace SOMapping.Controllers { public class HomeController : Controller { private ApplicationDbContext _applicationDbContext; public HomeController(ApplicationDbContext applicationDbContext) { _applicationDbContext = applicationDbContext; } public IActionResult Index() { var tags = _applicationDbContext.Tags.ToArray(); return View(); } } }

我有没有办法让这项工作成为可能,所以我不需要使用所有枚举空间的4倍空间?

枚举的基本类型和列的类型必须相同.
从更改枚举的基本类型开始:

public enum TagType: byte

你需要删除

... .HasColumnType("smallint");

然后列将是automaticaly tinyint,或者设置为manualy:

.HasColumnType("tinyint");

本文共计341个文字,预计阅读时间需要2分钟。

请问关于c的具体应用场景有哪些?

在OnModelCreating中尝试将枚举映射到smallint时,遇到以下异常:InvalidCastException: Unable to cast object of type 'System.Byte' to type 'System.Int32'. 这可能是由于在SQL Server中,int类型占用4个字节,而smallint只占用2个字节。

当我尝试在OnModelCreating中将枚举映射到smallint时,我得到以下异常:

请问关于c的具体应用场景有哪些?

InvalidCastException: Unable to cast object of type ‘System.Byte’ to type ‘System.Int32’.

我想这样做是因为在SQL Server中,int是4个字节而tinyint是1个字节.

相关代码:
实体:

namespace SOMapping.Data { public class Tag { public int Id { get; set; } public TagType TagType { get; set; } } public enum TagType { Foo, Bar } }

的DbContext:

using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; namespace SOMapping.Data { public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } public DbSet<Tag> Tags { get; set; } protected override void OnModelCreating(ModelBuilder builder) { builder.Entity<Tag>().Property(m => m.TagType).HasColumnType("smallint"); base.OnModelCreating(builder); } } }

查询:

using System.Linq; using Microsoft.AspNetCore.Mvc; using SOMapping.Data; namespace SOMapping.Controllers { public class HomeController : Controller { private ApplicationDbContext _applicationDbContext; public HomeController(ApplicationDbContext applicationDbContext) { _applicationDbContext = applicationDbContext; } public IActionResult Index() { var tags = _applicationDbContext.Tags.ToArray(); return View(); } } }

我有没有办法让这项工作成为可能,所以我不需要使用所有枚举空间的4倍空间?

枚举的基本类型和列的类型必须相同.
从更改枚举的基本类型开始:

public enum TagType: byte

你需要删除

... .HasColumnType("smallint");

然后列将是automaticaly tinyint,或者设置为manualy:

.HasColumnType("tinyint");