【问题标题】:EF core can't create Table, error in sql syntaxEF核心无法创建表,sql语法错误
【发布时间】:2020-11-14 05:18:23
【问题描述】:

我是 Entity 框架的新手,我不明白为什么它会给我一个 sql 语法错误,我认为使用 EFCore 的全部目的是处理 sql 语法并将所有 sql 查询抽象出来

这是我的模型:

  class Block 
    {
        public BlockHeader Header {get ;}
        public List<Transaction> Transactions {get;}

        public Block(List<Transaction> transactions, BlockHeader header)
        {
            Transactions= transactions;
            Header=header;           
        }    
        public Block(){
            
        }
    }
    public class BlockHeader
    {
        public byte[] Hash {get ;} 
        public byte[]  PreviousHash { get; }
        public DateTime timestamp { get; }
        public int version{ get; } 
        public byte[] MerkleRoot{ get; }
        public int SequenceNumber {get ; private set;}

        public BlockHeader (byte[] previoushash,int sequencenumber,List<Transaction> transactions)
        {
            timestamp = DateTime.Now;
            PreviousHash =  previoushash;
            version = 1;
            SequenceNumber = sequencenumber;
            MerkleRoot = ComputeMerkleRoot(transactions);
            Hash = ComputeBlockHash();

        }

这是我的数据库上下文类

        class BlockchainDB : DbContext
        {
            public BlockchainDB()
            {
                
            }

            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                optionsBuilder.UseMySQL("server=localhost;database = Blockchain ;user = root ; password = password");
            }
            protected override void OnModelCreating(ModelBuilder builder){
                builder.Entity<Block>(e => e.HasNoKey());
            }   
        }

当我添加迁移时,它会成功添加,但是当我使用此命令更新数据库时

dotnet ef database update

它输出这个错误:

Failed executing DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE `Block` (

);
MySql.Data.MySqlClient.MySqlException (0x80004005): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 3

【问题讨论】:

  • @Bizhan EntityFramework 是这样做的,不是我,我什至找不到错误的位置
  • 它试图创建一个没有任何属性的表,这似乎很不正确。您没有导航属性的设置器。如果您尝试封装它,请使用受保护的空构造函数和私有设置器。我很确定可以在其中一些指针中找到错误。错误位于生成的SQL中CREATE TABLE Block`(--如果为空,此方法将不起作用。);`
  • 尝试将 DbSet 添加到您的 DbContext 类中

标签: c# .net-core orm entity-framework-core mysql.data


【解决方案1】:

EFCore 实体需要设置器,否则不会设置引用。

我建议你写这样的东西来确保封装

public class Block 
{
  protected Block()
  {
    // empty constructor needed for EF Core
  }

  // Navigation properties 
  public virtual BlockHeader BlockHeader {get; private set;}

  // For collections 
  private readonly List<ChildEntity> _children = new List<ChildEntity>();
  public virtual IReadOnlyList<ChildEntity> Children => _children.ToList();

  // Encapsulated Business logic constructor 
  public Block(BlockHeader header, List<ChildEntity> children)
  {
    _children = children;
    BlockHeader = header;
  }
}

您也可以通过实体核心框架使用fluent api modelbuilder配置并显式映射关系。

【讨论】:

  • 这解决了,错误消失了,谢谢先生,现在我试图在我的上下文类中配置块头的关系,我有新的错误,但我想我可以找出问题所在,谢谢你先生
  • 很高兴我能帮上忙。 :) 祝你好运,如果块和块头之间是 1-1 关系,我建议使用 fluent api,否则你必须用 [ForeignKey] 属性注释依赖的子类。
  • 我用 fluent api 做的,非常感谢先生
猜你喜欢
  • 2018-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多