Considering the following C# code, I would like to know how can I use it to create a computed column so when querying I could do SELECT ID, MaskedID FROM dbo.Product
ALTER TABLE dbo.Product ADD MaskedID AS (<Utils.Mask(ID) ???>) PERSISTED;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class Util {
public string Mask(long id) {
var maskedId = $"do-something-with-{id}"; // it's way more complex than this
return maskedId;
}
}
Related
I have begun writing an ASP.NET Web API for an app that I am building. I have set up a MongoCRUD.cs class to save data from POST requests made by the app to a MongoDB database (and other CRUD actions).
I (following a beginner tutorial), also set up a Submission.cs model class to act as a blueprint for the objects I wanted to save to the database. However, now that I have implemented the InsertRecord() method in MongoCRUD.cs, I cannot see a use for this model.
MongoCRUD.cs:
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebAPI.Services
{
public class MongoCRUD
{
private IMongoDatabase db;
public MongoCRUD(string database)
{
var client = new MongoClient();
db = client.GetDatabase(database);
}
public void InsertRecord<T>(string table, T record)
{
var collection = db.GetCollection<T>(table);
collection.InsertOne(record);
}
}
}
Submission.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Web;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace WebAPI.Models
{
public class Submission
{
[BsonId]
public string SubId { get; set; }
public string Url { get; set; }
public string Text { get; set; }
}
}
SubmissionsController.cs:
using WebAPI.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebAPI.Services;
using System.IO;
using System.Web.Script.Serialization;
namespace WebAPI.Controllers
{
public class SubmissionsController : ApiController
{
MongoCRUD db = new MongoCRUD("myDb");
Submission[] submission = new Submission[] {
new Submission { SubId = "test", Url = "test", Text = "test" };
};
public IEnumerable<Submission> GetAllSubmissions()
{
//add MongoCRUD functionality for load record
return submission;
}
public IHttpActionResult GetSubmission(string id)
{
//add MongoCRUD functionality for load record
return Ok();
}
public IHttpActionResult PostSubmission(object body)
{
//validate body
db.InsertRecord("Submissions", body);
return Ok();
}
}
}
As you can see at PostSubmission(), the body of the POST request can be saved to the database directly, so my question is what is the benefit of using a model such as Submission.cs instead of just using the object type?
I'm having some trouble traversing the body object to access its values (for carrying out validation etc), so my only guess is that using a model makes it easier to access values?
object is the base type for all classes (See - https://learn.microsoft.com/en-us/dotnet/api/system.object?view=netcore-3.1).
C# is an object-orientated language so we try to model classes based on our business domain, this makes it easier to reason about the code.
Like you said you can have models that can be used to validate the incoming data into the controller, also you might want to add extra methods on the models that related to your business domain.
For example
class CreditCard
{
string CardNumber { get;set; }
string Csv { get;set; }
bool ValidateChecksum()
{ }
}
I have Profile table but profiles is in red and gives me an error that 'LoginDataContext' does not contain a definition for 'profiles'
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace practice
{
public partial class LoginDataContext
{
public Profile get_profileid(int id)
{
return this.profiles.SingleOrDefault(p => p.profile_id == id);
}
}
}
I'm assuming I'm missing a namespace declared on the top of my controller but I tried with 'using WebMatrix.Data;' and 'using System.IO;' that they are the usual suggestions for this error. It didn't work.
I'm trying to display more than one database table in the same view and this error is stopping me. Help please.
My Controller:
using System.Linq;
using System.Web.Mvc;
using KMS.Models;
using WebMatrix.Data;
using System.IO;
namespace KMS.Controllers
{
public class KMSController : Controller{
public ActionResult Index()
{
KMSConection cs = new KMSConection();
cs.Areas = (from o in db.Areas select o).Tolist();
cs.AreaTypes = (from o in db.AreaTypes select or).Tolist();
return View(cs);
}
}
}
My ViewModel Class:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using KMS.Models;
namespace KMS.Models
{
public class KMSConection: ApplicationDbContext
{
public DbSet<Area> Areas { get; set; }
public DbSet<AreaType> AreaTypes { get; set; }
}
}
Thanks!
Because you are not mentioning the db variable you have created into your KMSController. Try this code to fix the error:
using System.Linq;
using System.Web.Mvc;
using KMS.Models;
using WebMatrix.Data;
using System.IO;
namespace KMS.Controllers
{
public class KMSController : Controller{
public ActionResult Index()
{
//Insert the actual entity referred to your db variable.
//Probably something like:
//var db = Some Database Context
KMSConection cs = new KMSConection();
cs.Areas = (from o in db.Areas select o).Tolist();
cs.AreaTypes = (from o in db.AreaTypes select or).Tolist();
return View(cs);
}
}
}
I've searched alot but I can't find a solution for my problem:
I'm writing an Windows 8.1 App and I've a Class, which I want to Serialize, because I want to store the Data from this Class.
But I can't access the File-Class, Visual Studio still cannot recognize File as a class name.
Here's my Code:
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using Windows.Storage;
namespace Password.Data
{
class DataContainer
{
SortedDictionary<int, PasswordBox> Data { get; set; }
public int Key { get; set; }
public void Save(DataContainer a, string filename)
{
XmlSerializer ser = new XmlSerializer(typeof(DataContainer));
using (Stream s = File.OpenWrite(filename))
{
ser.Serialize(s, a);
}
}
}
}
How can I use the File-Class in this case? Or are there new Classes in order to tell my Stream that he has to write the data to file xy.
Or are there other ways to save a Dictionary?
Is it possible to Query the AspNetUsers table based on the keyword using N-Tier design modeled after:
Implementing a generic data access layer using Entity Framework
I created the following Interfaces in my DAL:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Library.Model;
namespace Library.DataAccessLayer
{
...
public interface IAspNetUserRepository : IGenericDataRepository<ApplicationUser>
{
}
public class AspNetUserRepository : GenericDataRepository<ApplicationUser>, IAspNetUserRepository
{
}
}
And the Following BLL entry:
using System;
using System.Collections.Generic;
using System.Linq;
using Library.DataAccessLayer;
using Library.Model;
namespace Library.BusinessLogicLayer
{
public interface IBusinessLogicLayer_AspNetUser
{
ApplicationUser GetAspNetUserByAspNetUserID(string _UserID);
}
public class BusinessLogicLayer_AspNetUser : IBusinessLogicLayer_AspNetUser
{
private readonly IAspNetUserRepository _AspNetUserRepository;
public BusinessLogicLayer_AspNetUser()
{
_AspNetUserRepository = new AspNetUserRepository();
}
public BusinessLogicLayer_AspNetUser(IAspNetUserRepository AspNetUserRepository)
{
_AspNetUserRepository = AspNetUserRepository;
}
public ApplicationUser GetAspNetUserByAspNetUserID(string _UserID)
{
return _AspNetUserRepository.GetSingle(u => u.Id.Equals(_UserID));
}
}
}
Now in the BLL file, the only Lambda expression does not contain Td so it errors out.
What is the correct model to use. The only information I can find is that ApplicationUser inherits Identity User.
What needs to be done or changed so I can get these fields added to the ApplicationUser model without it affecting the Code First portion for the database?
I finally found the answer. It was actually staring me right in the face.
simply use the following code to get the user(s) object:
var context = new ApplicationDbContext();
var user = context.Users.{Methods and Lambda expressions};
for example, say you need the existing KNOWN active user's UserName, use User.Identity.Name. And to get the active User's AspNetUsers database record:
var context = new ApplicationDbContext();
var user = context.Users.SingleOrDefault(u => u.UserName == User.Identity.Name);