Search code examples
c#.net-coreormentity-framework-coremysql.data

EF core can't create Table, error in sql syntax


im new to Entity framework and i dont understand why it is giving me an error in sql syntax , i thought the whole purpose of using EFCore is to handle the sql syntax and abstract all the sql queries away

here is my model :

  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();

        }

and here is my database Context class

        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());
            }   
        }

when i add a migration it adds it successfully but when i update the database using this command

dotnet ef database update

it outputs this error :

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

Solution

  • EFCore entities needs setters, otherwise the reference will not be set.

    I'd suggest you would write it something like this to ensure encapsulation

    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;
      }
    }
    

    Alternatively you can use the fluent api modelbuilder configuration through entity core framework and explicitly map the relations.