当我添加迁移时,我在.NET 7 Windows Forms应用程序中遇到了这个错误。

huangapple go评论69阅读模式
英文:

When I add migration I get this error with .NET 7 Windows Forms app

问题

namespace ManegmentSystemCompany.Data.Context
{
    public class ApplicationDbContext : DbContext
    {
        public DbSet<Catogry> Catogry { get; set; }
        public DbSet<Custemer> Custemer { get; set; }
        public DbSet<Employee> employees { get; set; }
        public DbSet<Pruducts> pruducts { get; set; }
        public DbSet<Stores> stores { get; set; }
        public DbSet<Supplier> suppliers { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer("Data Source=.;Initial Catalog=WhereHouseDb; Integrated Security=true");
        }
    }
}

After I run add-migration, I get this error. I could not find a fix for it - help me please.

No suitable constructor was found for entity type 'Font'. The following constructors had parameters that could not be bound to properties of the entity type:

Cannot bind 'info', 'context' in 'Font(SerializationInfo info, StreamingContext context)'
Cannot bind 'prototype', 'newStyle' in 'Font(Font prototype, FontStyle newStyle)'
Cannot bind 'family', 'emSize', 'style', 'unit' in 'Font(FontFamily family, float emSize, FontStyle style, GraphicsUnit unit)'
Cannot bind 'family', 'emSize', 'style', 'unit', 'gdiCharSet' in 'Font(FontFamily family, float emSize, FontStyle style, GraphicsUnit unit, byte gdiCharSet)'
Cannot bind 'family', 'emSize', 'style', 'unit', 'gdiCharSet', 'gdiVerticalFont' in 'Font(FontFamily family, float emSize, FontStyle style, GraphicsUnit unit, byte gdiCharSet, bool gdiVerticalFont)'
Cannot bind 'familyName', 'emSize', 'style', 'unit', 'gdiCharSet' in 'Font(string familyName, float emSize, FontStyle style, GraphicsUnit unit, byte gdiCharSet)'
Cannot bind 'familyName', 'emSize', 'style', 'unit', 'gdiCharSet', 'gdiVerticalFont' in 'Font(string familyName, float emSize, FontStyle style, GraphicsUnit unit, byte gdiCharSet, bool gdiVerticalFont)'
Cannot bind 'family', 'emSize', 'style' in 'Font(FontFamily family, float emSize, FontStyle style)'
Cannot bind 'family', 'emSize', 'unit' in 'Font(FontFamily family, float emSize, GraphicsUnit unit)'
Cannot bind 'family', 'emSize' in 'Font(FontFamily family, float emSize)'
Cannot bind 'familyName', 'emSize', 'style', 'unit' in 'Font(string familyName, float emSize, FontStyle style, GraphicsUnit unit)'


<details>
<summary>英文:</summary>

![enter image description here](https://i.stack.imgur.com/8y4Lu.png)

![enter image description here](https://i.stack.imgur.com/aTfVm.jpg)

![enter image description here](https://i.stack.imgur.com/A8KuA.jpg)

    namespace ManegmentSystemCompany.Data.Context
    {
        public class ApplicationDbContext : DbContext
        {
            public DbSet&lt;Catogry&gt; Catogry { get; set; }
            public DbSet&lt;Custemer&gt; Custemer { get; set; }
            public DbSet&lt;Employee&gt; employees { get; set; }
            public DbSet&lt;Pruducts&gt; pruducts { get; set; }
            public DbSet&lt;Stores&gt; stores { get; set; }
            public DbSet&lt;Supplier&gt; suppliers { get; set; }
    
            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                optionsBuilder.UseSqlServer(&quot;Data Source=.;Initial Catalog=WhereHouseDb; Integrated Security=true&quot;);
            }
        }
    }
    
After I run `add-migration`, I get this error. I could not find a fix for it - help me please.

&gt; No suitable constructor was found for entity type &#39;Font&#39;. The following constructors had parameters that could not be bound to properties of the entity type: 
&gt;
&gt; Cannot bind &#39;info&#39;, &#39;context&#39; in &#39;Font(SerializationInfo info, StreamingContext context)&#39;  
&gt; Cannot bind &#39;prototype&#39;, &#39;newStyle&#39; in &#39;Font(Font prototype, FontStyle newStyle)&#39;  
&gt; Cannot bind &#39;family&#39;, &#39;emSize&#39;, &#39;style&#39;, &#39;unit&#39; in &#39;Font(FontFamily family, float emSize, FontStyle style, GraphicsUnit unit)&#39;  
&gt; Cannot bind &#39;family&#39;, &#39;emSize&#39;, &#39;style&#39;, &#39;unit&#39;, &#39;gdiCharSet&#39; in &#39;Font(FontFamily family, float emSize, FontStyle style, GraphicsUnit unit, byte gdiCharSet)&#39;  
&gt; Cannot bind &#39;family&#39;, &#39;emSize&#39;, &#39;style&#39;, &#39;unit&#39;, &#39;gdiCharSet&#39;, &#39;gdiVerticalFont&#39; in &#39;Font(FontFamily family, float emSize, FontStyle style, GraphicsUnit unit, byte gdiCharSet, bool gdiVerticalFont)&#39;  
&gt; Cannot bind &#39;familyName&#39;, &#39;emSize&#39;, &#39;style&#39;, &#39;unit&#39;, &#39;gdiCharSet&#39; in &#39;Font(string familyName, float emSize, FontStyle style, GraphicsUnit unit, byte gdiCharSet)&#39;  
&gt; Cannot bind &#39;familyName&#39;, &#39;emSize&#39;, &#39;style&#39;, &#39;unit&#39;, &#39;gdiCharSet&#39;, &#39;gdiVerticalFont&#39; in &#39;Font(string familyName, float emSize, FontStyle style, GraphicsUnit unit, byte gdiCharSet, bool gdiVerticalFont)&#39;  
&gt; Cannot bind &#39;family&#39;, &#39;emSize&#39;, &#39;style&#39; in &#39;Font(FontFamily family, float emSize, FontStyle style)&#39;  
&gt; Cannot bind &#39;family&#39;, &#39;emSize&#39;, &#39;unit&#39; in &#39;Font(FontFamily family, float emSize, GraphicsUnit unit)&#39;  
&gt; Cannot bind &#39;family&#39;, &#39;emSize&#39; in &#39;Font(FontFamily family, float emSize)&#39;  
&gt; Cannot bind &#39;familyName&#39;, &#39;emSize&#39;, &#39;style&#39;, &#39;unit&#39; in &#39;Font(string familyName, float emSize, FontStyle style, GraphicsUnit unit)&#39;



</details>


# 答案1
**得分**: 1

没有找到适合实体类型 'Font' 的构造函数。

你是否希望将 Font 存储在数据库中?如果是的话,它需要一个 Entity Framework (EF) 能够用来创建实例的构造函数。有关规则的文档可以在这里找到:

你可以定义一个带有参数的构造函数,并在创建实体的实例时让 EF Core 调用这个构造函数。构造函数的参数可以绑定到已映射属性,也可以绑定到各种类型的服务,以便实现诸如延迟加载等行为。

如果你不希望将 Font 存储在数据库中,你需要将其排除在外。

[1]: https://learn.microsoft.com/en-us/ef/core/modeling/constructors
[2]: https://learn.microsoft.com/en-us/ef/core/modeling/entity-properties?tabs=data-annotations%2Cwithout-nrt#included-and-excluded-properties

<details>
<summary>英文:</summary>



&gt; No suitable constructor was found for entity type &#39;Font&#39; 

Do you expect Font to be stored in the database? If so, it needs a constructor that EF can use to create instances.  The rules are documented here: 

&gt; It&#39;s possible to define a constructor with parameters and have EF Core
&gt; call this constructor when creating an instance of the entity. The
&gt; constructor parameters can be bound to mapped properties, or to
&gt; various kinds of services to facilitate behaviors like lazy-loading.

[Entity types with constructors][1].

If you don&#39;t expect to store Font in the database, you need to [exclude it][2].


  [1]: https://learn.microsoft.com/en-us/ef/core/modeling/constructors
  [2]: https://learn.microsoft.com/en-us/ef/core/modeling/entity-properties?tabs=data-annotations%2Cwithout-nrt#included-and-excluded-properties

</details>



huangapple
  • 本文由 发表于 2023年7月23日 22:20:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76748725.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定