- Home /
Can not convert lamda expression to non delegate type
I am getting the error Assets/Resources/Scripts/Database/Database.cs(27,31): error CS1660: Cannot convert lambda expression' to non-delegate type
System.Linq.Expressions.Expression>' in the unity console but I am not receiving the same error in visual studio.
My Code is
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using Microsoft.EntityFrameworkCore;
using Npgsql.EntityFrameworkCore;
using Npgsql;
using System.Linq;
public class DatabaseManager : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql(@"Host=localhost;Database=entitycore;Username=Chevauchee;Password=Chevauchee");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<TileProperty>(entity =>
{
entity.ToTable("Tile");
entity.HasKey(e => e.x); //error occours here
entity.Property(e => e.y).IsRequired().HasColumnName("y"); //and here
entity.Property(e => e.terrain).IsRequired().HasColumnName("terrain"); //and here
entity.Property(e => e.domain).IsRequired().HasColumnName("domain"); //and here
});
}
public DbSet<TileProperty> Tile { get; set; }
}
Any advice would be appreciated
Unity uses a custom .Net framework that is equivalent to an older version (3.5, if I'm not mistaken), in which features of newer versions (that Visual Studio automatically uses) are not available, and that's why VS is not showing it as an error, but when Unity compiles it, it is.
However, these features are often only "syntactic sugar", that is simpler ways of writing something more complex, or they can be written in a different way. I am not proficient with Linq, but I think if you separate it into several steps, it might work.
Also, in Unity 2017.x an experimental version was introduced, which should be equivalent to .Net 4.6. You can switch to it in Edit -> Project Settings -> Player, and in Other settings scroll down to Configuration, and there use the first option, "Scripting Runtime Version".
Answer by Bunny83 · Jun 29, 2018 at 09:18 AM
When you use frameworks which are not part of Unity you should read the documentation carefully. I have never used the entity framework of MS but a quick google seach for the ModelBuilder showed that the "Entity" method returns an EntityTypeBuilder object. If you search for the signature of the HasKey method you will notice that it doesn't take a delegate as parameter but a simple params string array
HasKey(String[])
Sets the properties that make up the primary key for this entity type.
public virtual KeyBuilder HasKey(params string[] propertyNames)
Parameters
propertyNames
System.String[]
The names of the properties that make up the primary key.
Returns
KeyBuilder
An object that can be used to configure the primary key.
Likewise the Property method takes a single string value as parameter.
Furthermore the PropertyBuilder that is returned by the Property method (and carried on by the IsRequired method) does not have a HasColumnName method. Maybe this was added by another framework you use that is build on top of the MS framework.
However this question is not related to Unity at all and is lacking substantial information like framework versions, which frameworks you're using, etc... Since you already work with those frameworks you should have receive some sort of documentary which you should consult first.
Apart from that it probably should look something like that:
modelBuilder.Entity<TileProperty>(entity =>
{
entity.ToTable("Tile");
entity.HasKey("x");
entity.Property("y").IsRequired().HasColumnName("y");
entity.Property("terrain").IsRequired().HasColumnName("terrain");
entity.Property("domain").IsRequired().HasColumnName("domain");
});
As i said i don't know if the HasColumnName exists in your case. If not you may just want to remove it.
Thanks, for the help your solution worked. I believed it was an issue with unity because I received the following errors in the console but not in the IDE as shown above which I thought was odd
Your answer
![](https://koobas.hobune.stream/wayback/20220612165714im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How To Store and Act on Get Component Result in Linq Query? 1 Answer
How to return the first Texture from a List given a texture name (string) using Linq? 1 Answer
Linq List to Gui 0 Answers
C#, LINQ and Lists - help with 1 line of code? 1 Answer
Destroy a gameobject referenced in list and remove that element 2 Answers