// Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            // Get the Catalog for the SharePoint site
            BdcService service =
                SPFarm.Local.Services.GetValue <BdcService>(String.Empty);
            SPSite           site    = new SPSite("http://sp2016:2016/");
            SPServiceContext context = SPServiceContext.GetContext(site);
            AdministrationMetadataCatalog catalog =
                service.GetAdministrationMetadataCatalog(context);


            catalog.GetModels("EmployeeModel")?.ToList().ForEach(m => m.Delete());
            // Create a new Employee Model
            // NOTE: Assume that the "EmployeeModel" Model
            // does not already exist.
            Model EmployeeModel = Model.Create("EmployeeModel", true, catalog);


            // Make a new Employee LobSystem
            // NOTE: Assume that the "AdventureWorks" LobSystem
            // does not already exist.
            LobSystem adventureWorksLobSystem = EmployeeModel.OwnedReferencedLobSystems.Create("AdventureWorks", true, SystemType.Database);

            // Make a new AdventureWorks LobSystemInstance.
            LobSystemInstance adventureWorksLobSystemInstance = adventureWorksLobSystem.LobSystemInstances.Create("AdventureWorks", true);

            // Set the connection properties.
            adventureWorksLobSystemInstance.Properties.Add(
                "ShowInSearchUI", "");
            adventureWorksLobSystemInstance.Properties.Add(
                "DatabaseAccessProvider", "SqlServer");
            adventureWorksLobSystemInstance.Properties.Add(
                "RdbConnection Data Source", "SP2016");
            adventureWorksLobSystemInstance.Properties.Add(
                "RdbConnection Initial Catalog", "AdventureWorks2016");
            adventureWorksLobSystemInstance.Properties.Add(
                "AuthenticationMode", "RdbCredentials");
            adventureWorksLobSystemInstance.Properties.Add(
                "SsoProviderImplementation", "Microsoft.Office.SecureStoreService.Server.SecureStoreProvider, Microsoft.Office.SecureStoreService, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c");
            adventureWorksLobSystemInstance.Properties.Add(
                "SsoApplicationId", "Adventure");
            adventureWorksLobSystemInstance.Properties.Add(
                "RdbConnection Pooling", "true");

            // Create a new Employee Entity.
            Entity EmployeeEntity = Entity.Create(
                "Employee",
                "AdventureWorks",
                true,
                new Version("1.0.0.4"),
                10000,
                CacheUsage.Default,
                adventureWorksLobSystem,
                EmployeeModel,
                catalog);

            // Set the identifier to the EmployeeID column.
            EmployeeEntity.Identifiers.Create(
                "BusinessEntityID", true, "System.Int32");

            // Create the Finder Method,
            // i.e. the method to return all rows.
            CreateReadListMethod(catalog, EmployeeEntity);

            // Create the Specific Finder Method,
            // i.e. the method to return one row.
            CreateReadItemMethod(catalog, EmployeeEntity);

            // Validate the Employee Entity.
            ActivationError[] activationErrors =
                EmployeeEntity.Validate();

            // Check if the validation failed.
            if (activationErrors.Count() == 0)
            {
                // The validation was successful so publish the Employee Entity.
                EmployeeEntity.Activate();
            }
        }