首页 > 其他分享 >CSharp: donet 6 with Entity Framework Core 6

CSharp: donet 6 with Entity Framework Core 6

时间:2023-02-04 12:33:23浏览次数:51  
标签:donet set get Entity Framework modelBuilder Id Property public

 

    /// <summary>
    /// 
    ///Entity
    /// </summary>
    public class Product
    {

        public Product() { }    

        public int Id { get; set; }
        public string Name { get; set; }
        public int Quantity { get; set; }
        public float Price { get; set; }
    }

    /// <summary>
    /// Entity
    /// geovindu, Geovin Du
    /// </summary>
    public class Customer
    {

        public Customer() {

    
        }
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

    /// <summary>
    /// 
    /// </summary>
    public class ECommerceDbContext : DbContext
    {
        public DbSet<Product> Products { get; set; }
        public DbSet<Customer> Customers { get; set; }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer("Server=DESKTOP-NQK85G5\\GEOVIN2008;Database=geovinduCoreDBA;User Id=sa;Password=geovindu;");
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Product>();
            modelBuilder.Entity<Customer>();
            base.OnModelCreating(modelBuilder);
        }
    }


    /// <summary>
    /// 初始化创建表格
    /// </summary>
    [DbContext(typeof(ECommerceDbContext))]
    partial class ECommerceDbContextModelSnapshot : ModelSnapshot
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void BuildModel(ModelBuilder modelBuilder)
        {
#pragma warning disable 612, 618
            //ProductVersion
            modelBuilder
                .HasAnnotation("ProductVersion", "6.0.6")
                .HasAnnotation("Relational:MaxIdentifierLength", 128);

            SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1);
            //Customer
            modelBuilder.Entity("Customer", b =>
                {
                    b.Property<int>("Id")
                        .ValueGeneratedOnAdd()
                        .HasColumnType("int");

                    SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);

                    b.Property<string>("FirstName")
                        .IsRequired()
                        .HasColumnType("nvarchar(max)");

                    b.Property<string>("LastName")
                        .IsRequired()
                        .HasColumnType("nvarchar(max)");

                    b.HasKey("Id");

                    b.ToTable("Customers");
                });
            //Product
            modelBuilder.Entity("Product", b =>
                {
                    b.Property<int>("Id")
                        .ValueGeneratedOnAdd()
                        .HasColumnType("int");

                    SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);

                    b.Property<string>("Name")
                        .IsRequired()
                        .HasColumnType("nvarchar(max)");

                    b.Property<float>("Price")
                        .HasColumnType("real");

                    b.Property<int>("Quantity")
                        .HasColumnType("int");

                    b.HasKey("Id");

                    b.ToTable("Products");
                });
#pragma warning restore 612, 618
        }
    }

  

调用:

//初始化表格至数据库
ECommerceDbContext context = new();
await context.Database.MigrateAsync();

//
context = new ECommerceDbContext();
List<Customer> customers = new List<Customer>();
//添加
Customer customer=new Customer();
customer.FirstName= "涂";
customer.LastName = "聚文";
customers.Add(customer);
customer = new Customer();
customer.FirstName = "Du";
customer.LastName = "geovind";
customers.Add(customer);
context.Customers.AddRange(customers);
int k=context.SaveChanges();
if (k > 0)
{
    Console.WriteLine("ok");
}
//查询
var custer=context.Customers.FirstOrDefault();
Console.WriteLine(custer.FirstName+","+custer.LastName);

  

输出:

 

 

 

标签:donet,set,get,Entity,Framework,modelBuilder,Id,Property,public
From: https://www.cnblogs.com/geovindu/p/17091262.html

相关文章

  • GameFrameWork调研
     自上而下对应  Config提供了全局配置的读写功能,可用,但是没有意义,因为直接用1个全局静态类,直接读写就好了,更高效方便。而二进制功能需要的话,用scripObject方式也......
  • djangorestframework开发配置
    django和restframework结合。对api再次封装返回结果形如。非drf浏览器状态码返回基础模型封装分页格式调整{"msg":'success',"code":200,"data":[]}......
  • Cesium entity 跳动点 利用CallbackProperty
    startBounce(){constimage=require('./logo.png');constheight=44;conste={lng:116.4,lat:39.14,opts:{......
  • 基于IdentityServer的系统对接微信公众号
    业务需求公司有两个业务系统,A和B,AB用户之间属于多对一的关系,数据库里面也就是两张表,A表有个外键指向B。现在需要实现以下几个功能。A用户扫描B的二维码,填写相关的注册......
  • django rest framework 过滤时间操作
    https://www.nhooo.com/note/qa3v7f.html 通常我们会用到过滤两个时间段内的一些数据,当我们用djangorestframework的时候,他可以根据字段filter_fields进行过滤,但是......
  • django REST framework-序列化器
    djangoRESTframework简介构建RESTful风格API的一个基于django的web框架。drf框架,restframework框架。特性:强大的序列化器,可以高效的进行序列化和反序列化操作。极......
  • SeckillFramework项目开发错误记录
    Anerrorhappenedduringtemplateparsing(template:"classpathresource[templates/login.html]"-line34,col18)原因:由于添加了type="tel",删除恢复@mapp......
  • WCF错误:413 Request Entity Too Large
    在我们用WCF传输数据的时候,如果启用默认配置,传输的数据量过大,经常会出这个错误。WCF包含服务端与客户端,所以这个错误可能出现在服务端返回数据给客户端,或客户端传数据给服......
  • Django django-rest-framework-simplejwt
    Django(75)django-rest-framework-simplejwt「建议收藏」发布于2022-09-1611:56:13阅读 2440 大家好,又见面了,我是你们的朋友全栈君。 前言由于之前我们一......
  • 基于.net core的Azure function 如何使用.net framework所支持的编码
    在azurefunction中通过http请求call第三方api时,response返回是一堆中文乱码,发现数据格式使用的是"gb2312"编码因此在StreamReader的时候,增加了“gb2312”的encoding,代码......