0

I have the following code:

using Data;
using MyProject_API.Models.ViewModels;
using System.Linq;
using Utilities.Web.Authentication;

namespace MyProject_API.Services.EmailTemplates
{

    /// <summary>
    /// Defines the <see cref="EmailTemplatesSearch" />
    /// </summary>
    public class EmailTemplatesSearch
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="EmailTemplatesSearch"/> class.
        /// </summary>
        /// <param name="dataContext">The dataContext<see cref="DataContext"/></param>
        /// <param name="user">The user<see cref="MyProjectIdentity"/></param>
        public EmailTemplatesSearch(DataContext dataContext, MyProjectIdentity user)
        {
            _db = dataContext;
            _user = user;
        }

I've cleaned the code with help of CodeFormatter and get the following code:

namespace MyProject_API.Services.EmailTemplates
{
    using MyProject_API.Models.ViewModels;
    using System.Linq;
    using Utilities.Web.Authentication;

    /// <summary>
    /// Defines the <see cref="EmailTemplatesSearch" />
    /// </summary>
    public class EmailTemplatesSearch
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="EmailTemplatesSearch"/> class.
        /// </summary>
        /// <param name="dataContext">The dataContext<see cref="DataContext"/></param>
        /// <param name="user">The user<see cref="MyProjectIdentity"/></param>
        public EmailTemplatesSearch(DataContext dataContext, MyProjectIdentity user)
        {
            _db = dataContext;
            _user = user;
        }

As you can see, CodeFormatter looses using Data;. I've tried to add using Data; to the list of usings, but the code can't be compiled, DataContext which is in Data namespace can't be found. I fixed the problem moving using Data; outside from namespace, but it looks like a crutch. DataContext is defined like a

namespace Data
{
    public class DataContext

enter image description here

  • Why CodeFormatter looses using Data;?
  • Why using Data; doesn't work inside the namespace?

I've searched for similar questions: LINQ to SQL Designer Bug , Should 'using' directives be inside or outside the namespace? , etc., but i still don't understand how to solve my issues

Maxim Kitsenko
  • 2,042
  • 1
  • 20
  • 43
  • 2
    Do you have a conflicting namespace that includes `Data` in your solution/project/assembly? – maccettura Dec 04 '19 at 16:40
  • 2
    The output of the code formatter is wrong. Using statements should always be before a namespace. – JazzmanJim Dec 04 '19 at 16:42
  • What error do you see? Is it just DataContext does not exist in the current context? To me this feels like a conflict inside yout namespace. If you can see if there other data classes or similar? – panoskarajohn Dec 04 '19 at 16:48
  • 2
    @JazzmanJim `"Using statements should always be before a namespace"` -- will definitely need a source for that statement... – maccettura Dec 04 '19 at 16:49
  • @maccettura well it is the better practice -> https://stackoverflow.com/questions/125319/should-using-directives-be-inside-or-outside-the-namespace : but i do not like exclusive comments either. – panoskarajohn Dec 04 '19 at 16:56
  • Sometimes a version mismatch can cause this type of errors. If you have a project running .NET 4.5 and link to a project running 4.7 then the Code-Formatter/INtellisense will not notice and saying it's OK, but the real compiler complains - or vice versa. The same may appear on mismatching Build Configs like Any-CPU/x86. They can be linked but sometimes behave like invisible. Just something to check for compatibility. – Holger Dec 04 '19 at 19:40
  • @maccettura You are right! Thank you! I relied on tools like R# and Code Formatter, and i get into this pitfall. Codeformatter - didn't resolve namespaces correctly and removed correct using. R# - when i tried to fix 'Can't resolve symbol DataContext' with help of R#, R# didn't find appropriate namespace. When second time i was searching the conflicting namespace, according to your suggestion i used just simple search and found it. Thank you all guys! – Maxim Kitsenko Dec 05 '19 at 19:49

0 Answers0