Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Matt 359 posts 842 karma points
    Jan 19, 2023 @ 08:47
    Matt
    0

    Connect to another SQL Database

    Hi all,

    I have an umbraco 10 project which is working fine, however, I want a page which will link to another SQL database separate from Umbraco.

    This will just be getting some basic information from a database to display.

    Are there any guides around this? I'm using EntityFramework at the moment but cant get my head around it.

    Cheers

  • Michael Schwartz 6 posts 76 karma points
    Jan 19, 2023 @ 08:57
    Michael Schwartz
    0

    If you want to get data from an external source then I would suggest using the classic approach and creating a connection string and then access the DB with dotnet core capabilities .

    OR

    If you want to extend the current DB and have a better performance and managability then I would suggest this:

    https://docs.umbraco.com/v/10.x-lts/umbraco-cms/extending/database#data-stored-in-custom-database-tables
    
  • Matt 359 posts 842 karma points
    Jan 19, 2023 @ 12:15
    Matt
    0

    Hi,

    I did trying using EntityFramework

    This is what I have.

    Models/DepartmentModel.cs

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Linq;
    using System.Web;
    
    namespace Intranet.Models
    {
        [Table("Department")]
        public class Department
        {
            [Key]
            public int DeptID { get; set; }
    
            [DisplayName("Department Name")]
            public string DeptName { get; set; }
    
            public bool DeptActive { get; set; }
        }
    }
    

    Models/IdentityModels.cs

    using System.Data.Entity;
    using System.Security.Claims;
    using System.Threading.Tasks;
    using Microsoft.AspNet.Identity;
    using Microsoft.AspNet.Identity.EntityFramework;
    
    namespace Intranet.Models
    {
        // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit https://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
        public class ApplicationUser : IdentityUser
        {
            public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
            {
                // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
                var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
                // Add custom user claims here
                return userIdentity;
            }
        }
    
        public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
        {
            public DbSet<Department> Department { get; set; }
    
    
    
            public ApplicationDbContext()
                : base("serviceDirectory", throwIfV1Schema: false)
            {
            }
    
            public static ApplicationDbContext Create()
            {
                return new ApplicationDbContext();
            }
        }
    }
    

    Controllers/DepartmentController.cs

    using Microsoft.AspNetCore.Mvc;
    using Intranet.Models;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data.Entity;
    
    
    namespace Intranet.Controllers
    {
        public class DepartmentController : Controller
        {
            private ApplicationDbContext db;
    
            public DepartmentController()
            {
                db = new ApplicationDbContext();
            }
    
            // GET: Departments
            public ActionResult Index()
            {
                return View(db.Department.Where(d => d.DeptActive).ToList());
            }
    
            protected override void Dispose(bool disposing)
            {
                db.Dispose();
            }
        }
    }
    

    Then on my template page on Umbraco I have

    @using Umbraco.Cms.Web.Common.PublishedModels;
    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<ContentModels.TelephoneDirectory>
    @using ContentModels = Umbraco.Cms.Web.Common.PublishedModels;
    @model IEnumerable<Intranet.Models.Department>
    
    
    
    @{
        Layout = "Master.cshtml";
    }
    
    
    
    
    
    <form>
        <div class="form-group">
            <label for="sel1">Select Department (select one):</label>
            <select class="form-control" id="sel1">
                @foreach (var item in Model)
                {
                    <option>@Html.DisplayFor(m => item.DeptName)</option>
                }
            </select>
        </div>
    </form>
    

    But I'm getting the following error;

    foreach statement cannot operate on variables of type 'TelephoneDirectory' because 'TelephoneDirectory' does not contain a public instance or extension definition for 'GetEnumerator'

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies