Sunday 19 February 2017

AssetTracking 1 .asp core web application setup

//package manager console

PM> Add-Migration initial -Context AssetContext
PM> update-database -Context AssetContext

-------------------------------------------------------------
//~AssetTracking/appsetting.josn

{
  "ConnectionStrings": {
    "AssetConnection": "server=localhost;database=AssetTracking;Integrated Security=True;",
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-AssetTracking-62da8af5-da00-4796-b32b-b56cd08d19d9;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  }

}

--------------------------------------------------------------------
//~/AssetTracking/Startup.cs

 public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();

            services.AddMvc();

            var cs = Configuration.GetConnectionString("AssetConnection");
            services.AddDbContext<AssetContext>(options =>
                       options.UseSqlServer(cs));

            // Add application services.
            services.AddTransient<IEmailSender, AuthMessageSender>();
            services.AddTransient<ISmsSender, AuthMessageSender>();
        }

------------------------------------------------------------------
//~/Data/AssetContext.cs

using Domain;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Data
{
    public class AssetContext:DbContext
    {
        public AssetContext(DbContextOptions<AssetContext> options) : base(options){ }

        public DbSet<Asset> Assets { get; set; }
        public DbSet<AssetType> AssetType { get; set; }
        public DbSet<Manufacturer> Manufacturers { get; set; }
        public DbSet<Model> Models { get; set; }
    }
}

----------------------------------------------------------
//~/Domain/Asset.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Domain
{
    public class Asset
    {
        public int Id { get; set; }
        public string TagNumber { get; set; }
        public Nullable<int> AssetTypeId { get; set; }
        public Nullable<int> ManufacturerId { get; set; }
        public Nullable<int> ModelId { get; set; }
        public string Description { get; set; }
        public string AssignedTo { get; set; }
        public string SerialNumber { get; set; }

        public virtual AssetType AssetType { get; set; }
        public virtual Manufacturer Manufacturer { get; set; }
        public virtual Model Model { get; set; }
       
    }
}

--------------------------------------------------------------
//~/Domain/Model.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Domain
{
    public class Model
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public Nullable<int> ManufacturerId { get; set; }

        public virtual ICollection<Asset> Assets { get; set; }
        public virtual Manufacturer Manufacturer { get; set; }
    }
}

-----------------------------------------------------------------
//~/Domain/AssetType.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Domain
{
    public class AssetType
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public virtual ICollection<Asset> Assets { get; set; }
    }
}

----------------------------------------------------
//~/Domain/Manufacturer.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Domain
{
    public class Manufacturer
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public virtual ICollection<Asset> Assets { get; set; }
        public virtual ICollection<Model> Models { get; set; }
    }
}


reference:
http://stackoverflow.com/questions/38162227/asp-net-core-ef-add-migration-command-not-working
http://stackoverflow.com/questions/17127351/introducing-foreign-key-constraint-may-cause-cycles-or-multiple-cascade-paths
http://stackoverflow.com/questions/1030041/if-statement-inside-a-linq-select
http://stackoverflow.com/questions/6062192/there-is-already-an-open-datareader-associated-with-this-command-which-must-be-c

No comments:

Post a Comment