ObjectSet library is not being found? - asp.net-mvc-2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using ModelLayer.PocoModels;
using System.Data.Objects;
namespace ModelLayer
{
public class NorthwindDataContext : ObjectContext
{
private ObjectSet<Category> _categories;
private ObjectSet<Product> _products;
public NorthwindDataContext()
: base("name=NorthwindEntities",
"NorthwindEntities")
{
_categories = CreateObjectSet<Category>();
_products = CreateObjectSet<Product>();
}
}
}
In the above code I get an error as it cant find the ObjectSet class and gives me type or namespace not found error.While in the sample project it works fine it is under using System.Data.Objects.ObjectSet but I dont see that library in my current project? I am using asp.net mvc and .net 4.0. Does anyone have any good ideas ?

Make sure your project has a reference to System.Data.Entity.
You may also need references to System.Runtime.Serialization and System.Security.
Visual Studio adds these for you automatically when you add an EDMX file (ADO.NET Entity Data Model) to your project.

Related

Load database table as IOption<> at startup

.Net Core 3.1 application. ConfigureServices in Startup.cs sets up an EF Core 5 DbContext. One of the "static" tables I wish to load into memory and make available via a AddOptions to make the table injectible throughout the application (cheap man's caching). Same as BINDing from appsettings but using an EF Core database.
I already "seed" the database in Main by creating a scope and getting the DbContext service to run an initialization method. That technique won't work in this case because I am adding a service. Is there a way to read the DbContext while I am still in ConfigureServices? A way to add a service once the Host is built but not run?
How would you approach this?
Wouldn't you know. I post the question and kept reframing my search terms and digging and voila.
Using IConfigureOptions
So I created a class
using MyContexts;
using MyModels;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System.Linq;
public class LoadMySettings : IConfigureOptions<MySettings>
{
private readonly IServiceScopeFactory _serviceScopeFactory;
public LoadMySettings(IServiceScopeFactory serviceScopeFactory)
{
_serviceScopeFactory = serviceScopeFactory;
}
public void Configure(MySettings options)
{
using (var scope = _serviceScopeFactory.CreateScope())
{
var provider = scope.ServiceProvider;
using (var dbContext = provider.GetRequiredService<MyContext>())
{
options.MySettingsList = dbContext.MySettingsTable.ToList();
}
}
}
}
and embedded this in ConfigureServices:
services.AddSingleton>IConfigureOptions<MySettinmgs>, LoadMySettings>();

How to get DPI device to PCL in Xamarin. Forms? - Followup

Using Xamarin Forms Visual Studio 2019.
I need to get the DPI of the device.
This post has a solution:
How to get DPI device to PCL in Xamarin. Forms?
Implementing the solution gave problems:
When applying the answer above I get this error message:
The type or namespace name 'DependencyAttribute' could not be found
(are you missing a using directive or an assembly reference?)
In your android implementation, add a new class:
[assembly: Dependency(typeof(DisplayInfo))]
namespace .....
--I solved this compiler problem by replacing with:
using Xamarin.Essentials;
[assembly: Xamarin.Forms.Dependency(typeof(DisplayInfo))]
However when I compile and run the code, it falls over when I try consume this in the Xamarin Core Code:
int dpi = DependencyService.Get<IDisplayInfo>().GetDisplayDpi();
I get this runtime error on the above line:
System.NullReferenceException: Object reference not set to an instance
of an object
Any ideas? I would have added this question as a comment on the original, but as a new user do not have the points to do this... If someone could drop a comment on the original question's solution pointing it to this URL that would be helpful too.
So the answer to my own question:
The solution original offered said do this:
In your android implementation, add a new class:
[assembly: Dependency(typeof(DisplayInfo))]
namespace YourAppNamespace.Droid
{
public class DisplayInfo : IDisplayInfo
{
public int GetDisplayWidth()
{
(int)Android.App.Application.Context.Resources.DisplayMetrics.WidthPixels;
}
public int GetDisplayHeight()
{
(int)Android.App.Application.Context.Resources.DisplayMetrics.HeightPixels;
}
public int GetDisplayDpi()
{
(int)Android.App.Application.Context.Resources.DisplayMetrics.DensityDpi;
}
}
}
The problem I did not see anywhere in the Android code that initialised this class, and I was unsure how best to do this really.
So I followed guidance for implementing the interface directly in the Android MainActivity.cs file, and this worked.. The video I watched to help me on that is here:
https://www.youtube.com/watch?v=lgcnYDb6cRQ&t=19s

Can you help me detect missing directive (extension method not found)

I am starting to play with rx extension for .NET and downloaded some sources.
Basic examples works fine, however there is a function causing an error apparently because an extension method is not declared (should be imported with some directive)
The line of code causing the error is the 2nd one (list.Run) in the commented source (I commented to be able to compile):
/*
protected static void WriteListToConsole<T>(IEnumerable<T> list, string name) {
list.Run(
value => Console.WriteLine("{0} : {1}", name, value),
() => Console.WriteLine("{0} Completed", name));
}
*/
it looks like Rx defines somewhere an ext. method Run to IEnumerable but I am not importing it.
I installed Rx v2.0.3 SDK.msi , added references in my project to all System.Reactive.* I found and used:
using System;
using System.Reactive.Subjects;
using System.Reactive.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
I also tried use the Nuget rx.main and ix.main packages in my project but the error is always there.
Searching on google IEnumerable + Run gives me no info.
Any help appreciated.
Thanks
Hi I think that is a very old sample you are looking at.
This is now replaced with the extension method ForEach which can be found in the System.Linq namespace in the System.Interactive dll (IX-Main nuget I think).
You will also note that the OnCompleted function parameter is gone, cause well it was a silly idea. The new code should look like:
protected static void WriteListToConsole<T>(IEnumerable<T> list, string name)
{
list.ForEach(value => Console.WriteLine("{0} : {1}", name, value));
Console.WriteLine("{0} Completed", name);
}

Getting error while using adaptTo method of Sling while using Sling Models

We are trying a sample sling model implementation in AEM 6.0.
The sling model class without the imports is like this :
#Model(adaptables = Resource.class)
public class Header {
#Inject
private String link;
#Inject
private String text;
public String getLink() {
return link;
}
public String getText() {
return text;
}
}
The sling model is being called in the jsp using the following lines of code
<sling:adaptTo adaptable="${resource}" adaptTo="com.mysite.models.Header" var="model"/>
<h2>${model.link}</h2>
<h2>${model.text}</h2>
However we are getting the following error :
No tag "adaptTo" defined in tag library imported with prefix "sling"
We have imported the taglib using following statement:
<%#taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling" %>
Initially, the 2.2.0 version of Apache Sling JSP Tag Library bundle was present. We also tried uploading the 2.2.4 version, but it didn't help.
Can somebody please guide if anything else is required for adaptTo tag to be available?
Stumbled across this while trying to help a coworker debug a similar issue. At first I was able to replicate this behavior (AEM 6.1) by copying the segment from the Doc page:
<sling:adaptTo adaptable="${resource}" adaptTo="org.apache.sling.api.resource.ValueMap" var="myProps" />
What I found on my local was that our custom global.jsp file reference the old, pre-Granite version at /libs/foundation/global.jsp. Within that file I saw it had <%#taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling/1.0" %>. As a quick test I deleted the /1.0 at the end and refreshed the page and BAM, it worked.

ASP MVC 2 Data Annotations not working with VS2008/ASP 3.5?

I have added data annotations to a 'buddy' class as referenced in MS guidance. In particular, the [DisplayName ("Name")] does not seem to be affecting anything. My understanding is that the value assigned to the annotation should be referenced and used by the Html.LabelFor(m => m.Attribute) helper to display the label for the field.
Am I wrong on this?
I noticed in the view data class there is a strong-type view called EntityName+EntityName_Validation. Does that pull in additional components needed?
I tried creating a view using one of those types and there is no scaffolding present in the resulting view. Perhaps that is a different topic altogether. The UIHint doesn't seem to have any impact either.
As mentioned, this is ASP.NET 3.5 code, in VS2008. I am using Linq to SQL. Perhaps this disqualifies the full use of data annotations too.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace Sample.Models {
[MetadataType (typeof (SampleRequest_Validation))]
public partial class SampleRequest {
public class SampleRequest_Validation {
[DisplayName ("Description of Project:")]
[Required (ErrorMessage = "Project description is required.")]
[StringLength (500, ErrorMessage = "Project description cannot exceed 500 characters.")]
[UIHint ("TextArea")]
string ProjectDescription {get; set;}
I suspect I am missing some reference somewhere...
Thanks!
Okay, there was something missing...
I didn't declare the class attibutes as public, so of course the data annotations failed.
The last lisne from the code snippet above should read:
public string ProjectDescription {get; set;}
The smallest things...